def test_worldmap(): wmap = world.World(print_values=True, style=choice(list(styles.values()))) # wmap.js = ('http://l:2343/2.0.x/pygal-tooltips.js',) # wmap.add('1st', [('fr', 100), { # 'value': ('us', 10), # 'node': {'style': 'fill: red'} # } # ]) # wmap.add('2nd', [('jp', 1), ('ru', 7), ('uk', 0)]) # wmap.add('3rd', ['ch', 'cz', 'ca', 'cn']) # wmap.add('4th', {'jp': 12, 'bo': 1, 'bu': 23, 'fr': 34}) # wmap.add('5th', [{ # 'value': ('tw', 10), # 'label': 'First label', # 'xlink': 'http://google.com?q=tw', # }, { # 'value': ('bw', 20), # 'label': 'Second one', # 'xlink': 'http://google.com?q=bw', # 'node': {'style': 'fill: blue'} # }, { # 'value': ('mw', 40), # 'label': 'Last' # }]) wmap.add('_', {'us': 1}) wmap.add('-', {'us': 2}) wmap.add('.', {'us': 3}) wmap.title = 'World Map !!' wmap.value_formatter = lambda x: '%d%%' % x return wmap.render_response()
def main(args): filename = 'population_data.json' with open(filename) as f: pop_data = json.load(f) cc_pops_1, cc_pops_2, cc_pops_3 = {}, {}, {} for pop_dict in pop_data: if pop_dict['Year'] == '2010': country_name = pop_dict['Country Name'] population = int(float(pop_dict['Value'])) code = get_country_code(country_name) if code: if population < 10000000: cc_pops_1[code] = population elif population < 1000000000: cc_pops_2[code] = population else: cc_pops_3[code] = population wm_style = RS('#336699', base_style=LCS) wm = pywm.World(style=wm_style) wm.title = 'World Population in 2010, by Country' wm.add('0-10m', cc_pops_1) wm.add('10m-1bn', cc_pops_2) wm.add('>1bn', cc_pops_3) wm.render_to_file('World_population.svg') return 0
def world_map(): # Get data from API PAYLOAD = {"code": "ALL"} URL = "https://api.statworx.com/covid" RESPONSE = requests.post(url=URL, data=json.dumps(PAYLOAD)) # Convert to data frame DFRAME = pd.DataFrame.from_dict(json.loads(RESPONSE.text)) # Return a list of codes of countries and Representation and cases and deaths codes = [ code.lower() for code in ( DFRAME.filter(items=["code", "cases"]) .groupby("code", as_index=False) .sum()["code"] .to_list() ) ] Representation = ["cases", "cases_cum", "deaths", "deaths_cum"] cases = ( DFRAME.filter(items=["code", "cases"]).groupby("code").sum().to_dict()["cases"] ) deaths = ( DFRAME.filter(items=["code", "deaths"]) .groupby("code") .sum() .to_dict()["deaths"] ) # Initial the World Map WorldMap = world.World( show_legend=False, pretty_print=True, human_readable=True, style=DarkStyle, tooltip_position=None, ) # Bind the code of country to the number of its cases and deaths dict_cases_deaths = {} for code in codes: dict_cases_deaths[code] = { "Cases: {}, Deaths: {}".format(cases[code.upper()], deaths[code.upper()]) } # Plot the data WorldMap.title = "Current Statistics of Deaths and Cases; updated every day" WorldMap.add("General Statistics", dict_cases_deaths) fig = WorldMap.render_data_uri() return render_template("world_map.html", fig=fig)
# Author: Bojan G. Kalicanin # Date: 27-Dec-2016 # Plot population of the three countries in North America. import pygal.maps.world as worldmap wm = worldmap.World() wm.title = 'Populations of Countries in North America' wm.add('North America', {'ca': 34126000, 'us': 309349000, 'mx': 113423000}) wm.render_to_file('na_populations.svg')
with open(data_path, "r") as f: csvdt = reader(f) for row in csvdt: if len(row) > 56: code = alpha_two(row[1]) if code is not None: code = code.lower() if code in cuty: if isNum(row[55]): co2[code] = float(row[55]) else: print("Leaving out", row[0]) else: print("Leaving out", row[0]) nodata = dict() for code in cuty: if code not in co2: nodata[code]='?' wmp = pw.World() wmp.force_uri_protocol = "http" wmp.title = "CO2 emissions in 2011" wmp.add("Known data", co2) wmp.add("No data", nodata) wmp.style = Style(colors=("#B22222", "#A9A9A9"), legend_font_size=10, tooltip_font_size=8) wmp.render() wmp.render_to_file("CO2_emissions.svg") # # Open browser window open_new_tab("file://" + path.realpath("CO2_emissions.svg"))
# 打印每个国家2016年的GDP for gdp_dict in gdp_data: if gdp_dict['Year'] == 2016: country_name = gdp_dict['Country Name'] gdp = float(gdp_dict['Value']) code = get_country_code(country_name) if code: cc_gdps[code] = gdp # 根据gdp值将所有的国家分成三组 cc_gdps_1, cc_gdps_2, cc_gdps_3 = {}, {}, {} for cc, gdp in cc_gdps.items(): if gdp < 10000000000: cc_gdps_1[cc] = gdp elif gdp < 100000000000: cc_gdps_2[cc] = gdp else: cc_gdps_3[cc] = gdp # 看看每组分别包含多少个国家 # print(len(cc_gdps_1), len(cc_gdps_2), len(cc_gdps_3)) wm_style = RS('#d8886d', base_style=LCS) # 让Pygal使用一种基色 wm = pmw.World(style=wm_style) wm.title = 'World GDP in 2016, by Country' wm.add('0-10bn', cc_gdps_1) wm.add('10bn-100bn', cc_gdps_2) wm.add('>100bn', cc_gdps_3) wm.render_to_file('diagram/Grouped_World_GDP_2016.svg')
import pygal.maps.world as maps from populationData import getCountryCode map = maps.World() map.title = "Americas" map.add("North America", ['us', 'mx', 'ca']) map.add('Central America', ['bz', 'cr', 'gt', 'hn', 'ni', 'pa', 'sv']) map.add('South America', [ 'ar', 'bo', 'br', 'cl', 'co', 'ec', 'gf', 'gy', 'pe', 'py', 'sr', 'uy', 've' ]) map.render_to_file("americas.svg")
Сгруппируем страны по 3 уровням населения 1) Менее 10 миллионов 2) От 10 миллионов до миллиарда 3) Более миллиарда """ cc1, cc2, cc3 = {}, {}, {} for cc, pop in cc_populations.items(): if pop < 10000000: cc1[cc] = pop elif pop < 1000000000: cc2[cc] = pop else: cc3[cc] = pop """ RotateStyle принимает аргумент - цвет в ШЕСТНАДЦАТИРИЧНОМ ФОРМАТЕ RGB Сначала пишется решется # Затем шесть цифр Первые две цифры - количество красного (Red) Вторые две цифры - количество зеленого (Green) Третьи две цифры - количество синего (Blue) """ style = RS( "#336699", base_style=LCS ) #LCS - LightColorizedStyle из импорта - осветляет цфета оформления немного wm = maps.World(style=style) wm.title = "World population if 2010" wm.add("0-10m", cc1) wm.add("10m-1b", cc2) wm.add("1b+", cc3) wm.render_to_file("world_2010.svg")
import pygal.maps.world as pmw wm = pmw.World() wm.title = 'Populations of Countries in North America' wm.add('North America', {'ca': 34126000, 'us': 309349000, 'mx': 113423000}) wm.render_to_file('na_populations.svg')
filename = 'data/population_data.json' with open(filename) as f: pop_data = json.load(f) cc_populations = {} for pop_dict in pop_data: if pop_dict['Year'] == '2010': country_name = pop_dict['Country Name'] population = int(float(pop_dict['Value'])) code = get_country_code(country_name) if code: cc_populations[code] = population cc_pops_1, cc_pops_2, cc_pops_3 = {}, {}, {} for cc, pop in cc_populations.items(): if pop < 10000000: cc_pops_1[cc] = pop elif pop < 1000000000: cc_pops_2[cc] = pop else: cc_pops_3[cc] = [pop] wm_style = LightColorizedStyle wm = hehe.World(style=wm_style) wm.title = 'just world map' wm.add('0-10m', list(cc_pops_1)) wm.add('10m-1bn', list(cc_pops_2)) wm.add('>1bn', list(cc_pops_3)) wm.render_to_file('worldpopulations_map_2.svg')
from pygal.style import RotateStyle as RS, LightColorizedStyle as LCS from get_country_code import get_cc # Get data from json file filename = 'chap16\\world_map\\world_data\\world_gdp.json' with open(filename) as f: pop_data = json.load(f) # Puts country code and gdp in dict country_gdps = {} for pop_dict in pop_data: if pop_dict['Year'] == 2016: # Gets country code country_name = pop_dict['Country Name'] code = get_cc(country_name) # Gets gdp gdp = int(float(pop_dict['Value'])) # Appends code and gdp country_gdps[code] = gdp # Creates map wm = pywm.World() wm.title = 'World GDP by Country' wm.add('2016 GDP', country_gdps) wm.render_to_file('world_gdp.svg')
#! /usr/bin/env python3.7 # -*- coding: utf-8 -*- import pygal.maps.world as WMap wm = WMap.World() wm.title = 'Populations of Countries in North America' wm.add('North America', {'ca': 34126000, 'us': 309349000, 'mx': 113423000}) wm.render_to_file('na_populations.svg')
country_name = pop_dict['Country Name'] code = get_cc(country_name) if not code: print(country_name) # Gets population population = int(float(pop_dict['Value'])) # Appends code and population country_populations[code] = population # Adds coutntry to 1 of 3 dicts based on their population cc_pops_1, cc_pops_2, cc_pops_3 = {}, {}, {} for cc, pop in country_populations.items(): if pop < 10000000: cc_pops_1[cc] = pop elif pop < 1000000000: cc_pops_2[cc] = pop else: cc_pops_3[cc] = pop # Creates map wm_style = RS('#336699', base_style=LCS) wm = pywm.World(style=wm_style) wm.title = 'World Population in 2010 by Country' wm.add('0-10m', cc_pops_1) wm.add('10m-1b', cc_pops_2) wm.add('>1b', cc_pops_3) wm.render_to_file('world_population.svg')
import pygal.maps.world as wp wm = wp.World() wm.title = 'populatin of countries in north america' wm.add('north america', {'ca': 34126000, 'us': 309349000, 'mx': 113423000}) wm.render_to_file('na_population.svg')
import pygal import pygal.maps.world as pw from country_codes import get_country_code import csv #导入数据 filename = 'gdp.csv' with open(filename, ) as f: gdp_data = csv.reader(f) for i in range(5): header = next(gdp_data) #获取每个国家2016年的GDP,做一个字典 gdps_dict = {} for gdp_dict in gdp_data: try: country_name = gdp_dict[0] gdp = int(gdp_dict[61]) print(int(gdp_dict[61])) except ValueError: print('Error') else: code = get_country_code(country_name) if code: gdps_dict[code] = gdp wg = pw.World() wg.title = '世界各国GDP' wg.add('2016', gdps_dict) wg.render_to_file('各国GDP.svg')
import pygal.maps.world as pyg wm = pyg.World() wm.title = 'North,Central,and South America' wm.add('North America', ['ca', 'mx', 'us']) wm.add('Central America', ['bz', 'cr', 'gt', 'hn', 'ni', 'pa', 'sv']) wm.add('South America', [ 'ar', 'bo', 'br', 'cl', 'co', 'ec', 'gf', 'gy', 'pe', 'py', 'sr', 'uy', 've' ]) wm.render_to_file('americas.svg')
import pygal.maps.world as pyWorld wm = pyWorld.World() wm.title = 'North, Cenrtal,and Sounth America' #add为添加突出标签,字符串为标签名字,列表为该标签的国家代码,如果用字典的话可以显示数值 wm.add('North America', {'ca': 3412600, 'mx': 113423000, 'us': 309349000}) wm.add('Central America', ['bz', 'cr', 'gt']) wm.render_to_file('americas.svg')
import pygal from pygal.maps import world as world worldmap_chart = world.World() worldmap_chart.title = "Some countries" worldmap_chart.add("F countries", ['fr', 'fi']) worldmap_chart.add('U countries', ['ua', 'ug', 'us', 'uy', 'uz']) worldmap_chart.add('M countries', [ 'ma', 'mc', 'md', 'me', 'mg', 'mk', 'ml', 'mm', 'mn', 'mo', 'mr', 'mt', 'mu', 'mv', 'mw', 'mx', 'my', 'mz' ]) worldmap_chart.render_to_file('bar_chart.svg')
import pygal.maps.world as maps wm = maps.World() wm.title = 'North, Central and South America' wm.add('North America', ['ca', 'mx', 'us']) wm.add('Central America', ['bz,', 'cr', 'gt', 'hn', 'ni', 'pa', 'sv']) wm.add('South America', [ 'ar', 'bo', 'br', 'cl', 'co', 'ec', 'gf', 'gy', 'pe', 'py', 'sr', 'uy', 've' ]) wm.render_to_file('graphs/americas.svg')
'''Practice plotting numerical data on a map''' import pygal.maps.world as py wm = py.World() wm.title = 'Populations of Countries in North America' #pass a dictionary as the second argument wm.add('North America', {'ca': 34126000, 'us': 309349000, 'mx': 113423000}) #pygal automatically use the numbers to shade the countries from light to dark wm.render_to_file('na_populations.svg')
with open(filename) as f: pop_data = json.load(f) cc_populations = {} for pop_dict in pop_data: if pop_dict['Year'] == '2010': country_name = pop_dict['Country Name'] population = int(float(pop_dict['Value'])) code = get_country_code(country_name) if code: cc_populations[code] = population # Group the countries into 3 population levels. cc_pops_1, cc_pops_2, cc_pops_3 = {}, {}, {} for cc, pop in cc_populations.items(): if pop < 10000000: cc_pops_1[cc] = pop elif pop < 1000000000: cc_pops_2[cc] = pop else: cc_pops_3[cc] = pop # See how many countries are in each level. print(len(cc_pops_1), len(cc_pops_2), len(cc_pops_3)) wm_style = RotateStyle('#336699', base_style=LightColorizedStyle) wm = pyworld.World(style=wm_style) wm.title = 'World Population in 2010, by Country' wm.add('0-10m', cc_pops_1) wm.add('10m-1bn', cc_pops_2) wm.add('>1bn', cc_pops_3) wm.render_to_file('data_visualisation/results/world_population.svg')
import pygal.maps.world as pmap wm = pmap.World() wm.title = 'North, Central, and South America' wm.add('North America', ['ca', 'mx', 'us']) wm.add('Central America', ['bz', 'cr', 'gt', 'hn', 'ni', 'pa', 'sv']) wm.add('South America', ['ar', 'bo', 'br', 'cl', 'co', 'ec', 'gf', 'gy', 'pe', 'py', 'sr', 'uy', 've']) wm.render_to_file('americas.svg')
for pop_dict in pop_data: if pop_dict["Year"] == '2010': country_name = pop_dict["Country Name"] population = int(float(pop_dict["Value"])) country_code = get_country_code(country_name) if country_code: cc_populations[country_code] = population #根据人口数量,对国家进行分组:少于1000w,1000w~10亿,10亿以上 cc_pops_1, cc_pops_2, cc_pops_3 = {}, {}, {} for code, pops in cc_populations.items(): if pops < 10000000: cc_pops_1[code] = pops elif pops < 1000000000: cc_pops_2[code] = pops else: cc_pops_3[code] = pops print(len(cc_pops_1), len(cc_pops_2), len(cc_pops_3)) #制定地图使用同一个颜色基调,我们选择一个style wm_style = RotateStyle('#336699', base_style=LCS) #16进制的RGB颜色,前两位表示red,中间两位绿,最后两位表示蓝 wm = WMap.World(style=wm_style) wm.title = 'World Population in 2010, by country' wm.add('10m', cc_pops_1) wm.add('10m-1bn', cc_pops_2) wm.add('>1bn', cc_pops_3) wm.render_to_file('world_population.svg')
# -*- coding: utf-8 -*- ''' Created on 2018年1月25日 @author: Jeff Yang ''' # 绘制地图的模块,有改动,书上的pygal.Worldmap()已经无法使用 from pygal.maps import world wm = world.World() wm.title = 'North, Central, and South America' # add()方法接收一个标签和一个列表,后者包含要突出的国家的国别码 # 每次调用add()都将为指定的国家选择一种新颜色,并在图标左边显示该颜色和指定的标签 wm.add('North America', ['ca', 'mx', 'us']) wm.add('Central America', ['bz', 'cr', 'gt', 'hn', 'ni', 'pa', 'sv']) wm.add('South America', [ 'ar', 'bo', 'br', 'cl', 'co', 'ec', 'gf', 'gy', 'pe', 'py', 'sr', 'uy', 've' ]) wm.render_to_file('americas.svg')
if pop_dict['Year'] == '2010': country_name = pop_dict['Country Name'] population = int(float(pop_dict['Value'])) code = get_country_code(country_name) if code: cc_populations[code] = population # else: # print('ERROR - ' + country_name) # 根据人口数量将所有的国家分成三组 cc_pops_1, cc_pops_2, cc_pops_3 = {}, {}, {} for cc, pop in cc_populations.items(): if pop < 10000000: cc_pops_1[cc] = pop elif pop < 1000000000: cc_pops_2[cc] = pop else: cc_pops_3[cc] = pop # 看看每组分别包含多少个国家 print(len(cc_pops_1), len(cc_pops_2), len(cc_pops_3)) wm_style = RS('#336699', base_style=LCS) wm = pgl.World(style=wm_style) wm.title = 'World Population in 2010, by Country' wm.add('0-10m', cc_pops_1) wm.add('10m-1bn', cc_pops_2) wm.add('>1bn', cc_pops_3) wm.render_to_file('world_population.svg')
import pygal.maps.world as pygalW wm = pygalW.World() wm.title = "North, Central and South America" wm.add('North America', ['ca', 'mx', 'us']) wm.add('Central America', ['bz', 'cr', 'gt', 'hn', 'ni', 'pa', 'sv']) wm.add('South America', [ 'ar', 'bo', 'br', 'cl', 'co', 'ec', 'gf', 'gy', 'pe', 'py', 'sr', 'uy', 've' ]) wm.render_to_file('Using_JSON/americas.svg')
with open(filename) as f: gdp_data = json.load(f) # Build a dictionary of GDP Data... global_gdp = {} for gdp_dict in gdp_data: if gdp_dict['Year'] == '2014': country_name = gdp_dict['Country Name'] gdp_value = int(float(gdp_dict['Value'])) code = get_country_code(country_name) if code: global_gdp[code] = gdp_value # Group countries into 3 groups by GDP gdp_pop1, gdp_pop2, gdp_pop3 = {}, {}, {} for cc, gdp in global_gdp.items(): if gdp > 10**11: gdp_pop1[cc] = gdp # 100 Billion elif gdp > 10**10: gdp_pop2[cc] = gdp # 10 Billion else: gdp_pop3 = gdp # Less than 10 Billion # Display world map with GDP data wm = WorldMap.World() wm.title = 'World GDP in 2014, by Country (USD)' wm.add('GDP > $100B', gdp_pop1) wm.add('GDP > $10B', gdp_pop2) wm.add('GDP < $10B', gdp_pop3) wm.render_to_file('Global GDP 2014.svg')
#!/usr/bin/env python # encoding=utf-8 import pygal.maps.world as hehe wm = hehe.World() wm.title = "just world" wm.add('North American', ['ca', 'mx', 'us']) wm.add('Central American', ['bz', 'cr', 'gt', 'hn', 'ni', 'pa', 'sv']) wm.add('South American', [ 'ar', 'bo', 'br', 'cl', 'co', 'ec', 'gf', 'gy', 'pe', 'py', 'sr', 'uy', 've' ]) wm.render_to_file('american.svg')
# build a dictionary of population population data cc_populations = {} for pop_dict in pop_data: if pop_dict['Year'] == '2010': country_name = pop_dict['Country Name'] population = int(float(pop_dict['Value'])) code = get_country_code(country_name) if code: cc_populations[code] = population # group the countries into 3 population levels: 0-10m, 10m-1bn, >1bn cc_pop_1, cc_pop_2, cc_pop_3 = {}, {}, {} for cc in cc_populations: pop = cc_populations[cc] if pop < 10000000: cc_pop_1[cc] = pop elif pop < 1000000000: cc_pop_2[cc] = pop else: cc_pop_3[cc] = pop # create population map of the world by get_country wm_style = RotateStyle('#115599', base_style=LCS) wm = py.World(style=wm_style) wm.title = ('World population in 2010, by country') wm.add('0-10m', cc_pop_1) wm.add('10m-1bn', cc_pop_2) wm.add('>1bn', cc_pop_3) wm.render_to_file('world_population.svg')
import pygal.maps.world as pgl # pygal.Worldmap()语句相应模块更改为pygal.maps.world。World() wm = pgl.World() wm.title = 'North, Central, and South America' wm.add('North America', ['ca', 'mx', 'us']) wm.add('Central America', ['bz', 'cr', 'gt', 'hn', 'ni', 'pa', 'sv']) wm.add('South America', [ 'ar', 'bo', 'br', 'cl', 'co', 'ec', 'gf', 'gy', 'pe', 'py', 'sr', 'uy', 've' ]) wm.render_to_file('americas.svg')