def world_map_test(): map_style = RotateStyle('#44ff77') map = World(style=map_style, value_formatter=lambda x: '{}'.format(x) + u'国家') map.title = 'world map scope' country_list = [] chinese_name = { 'United States': u'大美利坚', 'United Kingdom': u'大英帝国', 'France': u'法兰西', 'Italy': u'意大利', 'Japan': u'东瀛', 'Germany': u'德意志', 'Canada': u'加拿大' } detail_dict = {} print(type(chinese_name)) for code, name in COUNTRIES.items(): if name in ('United States', 'United Kingdom', 'France', 'Italy', 'Japan', 'Germany', 'Canada'): country_list.append(code) detail_dict[code] = chinese_name[name] print(code) #if name == 'Russian Federation': # unique=code for x in detail_dict.items(): map.add(u'G7国家', ) #map.add(u'G8国家',country_list+[unique]) map.render_to_file('/Users/qmp/Desktop/map1.svg')
def world_population(): filename = "population_data.json" with open(filename) as f: pop_data = json.load(f) cc_pop = {} 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_pop[code] = population #grouping countries into 3 pop levels cc_pops1, cc_pops2, cc_pops3 = {}, {}, {} for cc, pop in cc_pop.items(): if pop < 10000000: cc_pops1[cc] = pop elif pop < 1000000000: cc_pops2[cc] = pop else: cc_pops3[cc] = pop print(len(cc_pops1), len(cc_pops2), len(cc_pops3)) #building a world map wm_style = RotateStyle("#336699", base_style=LightColorizedStyle) wm = World(style=wm_style) wm.title = "World Population in 2010, by Country" wm.add('0-10m', cc_pops1) wm.add('10m-1bn', cc_pops2) wm.add('>1bn', cc_pops3) wm.render_to_file('world_population.svg')
def main(): # Get dict of code to population at year 2010 for plotting world_pop_2010 = build_pop_dict("population_data.json", 2010) # Divide dict into three smaller dicts corresponding to population levels (low, medium, high) low_pop_2010, medium_pop_2010, high_pop_2010 = {}, {}, {} for code, pop in world_pop_2010.items(): if pop < 10000000: low_pop_2010[code] = pop elif pop < 1000000000: medium_pop_2010[code] = pop else: high_pop_2010[code] = pop # Create pygal maps plotting object map_style = ps.DarkSolarizedStyle wm = World(style=map_style) wm.title = 'World populations' # Add the shading for countries in each population level wm.add('0-10m', low_pop_2010) wm.add('10m-1b', medium_pop_2010) wm.add('>1b', high_pop_2010) # Render graph to svg file wm.render_to_file('world.svg')
def one_country_map(): '''一国地图''' wm_o = World() wm_o.force_uri_protocol = 'http' wm_o.title = '世界地图(中国)' wm_o.add('China', {'中国': 'cn'}) wm_o.render_to_file('country_map.svg')
def world_country_map(): '''世界各国地图''' wm_c = World() wm_c.force_uri_protocol = 'http' wm_c.title = '世界地图' for code, name in COUNTRIES.items(): # print(name, code) wm_c.add(name, code) wm_c.add('Yemen', {'ye': 'Yemen'}) wm_c.render_to_file('world_map.svg')
def csv_data_handle(): with open( '/Users/qmp/Downloads/API_TX.VAL.TECH.MF.ZS_DS2_en_csv_v2/API_TX.VAL.TECH.MF.ZS_DS2_en_csv_v2.csv' ) as f: reader = csv.reader(f) #print(next(reader)) map_style = RotateStyle('#4455ee', base_style=LightStyle) map = World() info_dict = {} for x in reader: if len(x) == 62: print(x[0], x[1], x[-2]) code = counrty_list_show(x[0]) info_dict[code] = float((x[-2])) if x[-2] != '' else None map.title = 'High-technology exports (% of manufactured exports)' map.add('', info_dict) map.render_to_file('/Users/qmp/Desktop/High_Tech_map.svg')
def json_data_handle(): gdp_style = RotateStyle('#5555ff') map = World(style=gdp_style) package = Package('/Users/qmp/Downloads/gdp.zip') resources = package.descriptor['resources'] resourceList = [resources[x]['name'] for x in range(0, len(resources))] #print(resourceList) data = package.resources[0].read() show_dict = {} for x in data: if x[2] == 2016: code = counrty_list_show(x[0]) #print(code,x) gdp = long(x[-1]) print(gdp) show_dict[code] = gdp print(show_dict) map.title = u'2016世界所有经济体GDP' map.add(u'经济体', show_dict) map.render_to_file('/Users/qmp/Desktop/map_gdp.svg')
def clickedBtn(self): try: global capitala capitala = self.label_capitala.text() if capitala: path = easygui.filesavebox(msg="", title="Salvati datele", default=selected_country + ".txt", filetypes=None) # salvare date in fisier txt file = open(path, "w") file.write("Tara: " + tara + "\n") file.write("Continent: " + continent + "\n") file.write("Capitala: " + capitala + "\n") file.write("Populatie: " + populatie + "\n") file.write("Suprafata: " + suprafata + "\n") file.write("Moneda: " + moneda + "\n") file.write("Ora GMT: " + timezone + "\n") file.write("Provincii: " + str(provincii) + "\n") file.write( "All data: " + json.dumps(countries.get_all_info_for_country(tara)) + "\n") file.close() # desenam harta lumii si o salvam worldmap_chart = World() worldmap_chart.title = tara worldmap_chart.add( 'Tara aleasa', [countries.get_country_code_from_country(tara)]) path = path[:len(path) - 4] print(path) worldmap_chart.render_to_file(path + ".svg") else: self.prompt_message("Trebuie sa selectati o tara!", "Eroare") except Exception as ex: print(ex)
def render_world_map(gdpinfo, plot_countries, year, map_file): """ Inputs: gdpinfo - A GDP information dictionary plot_countries - Dictionary whose keys are plot library country codes and values are the corresponding country name year - String year to create GDP mapping for map_file - Name of output file to create Output: Returns None. Action: Creates a world map plot of the GDP data for the given year and writes it to a file named by map_file. """ worldmap_chart = World() worldmap_chart.title = 'GDP by Country (Log Scale) for ' + year gdp_dict, missing_data, missing_data_year = build_map_dict_by_name(gdpinfo, plot_countries, year) worldmap_chart.add('GDP for '+year, gdp_dict) worldmap_chart.add('Missing '+year+' data', missing_data_year) worldmap_chart.add('No record', missing_data) worldmap_chart.render_to_file(map_file)
class PWorldMap(object): ''' classdocs ''' def __init__(self, title): ''' Constructor ''' self.wmap = World() self.wmap.title = title def sample(self): self.wmap.add('F countries', ['fr', 'fi']) self.wmap.add('M countries', [ 'ma', 'mc', 'md', 'me', 'mg', 'mk', 'ml', 'mm', 'mn', 'mo', 'mr', 'mt', 'mu', 'mv', 'mw', 'mx', 'my', 'mz' ]) self.wmap.add('U countries', ['ua', 'ug', 'us', 'uy', 'uz']) self.wmap.add('North America', { 'ca': 84949494949, 'mx': 494794164, 'us': 99794616 }) def render(self, filename=None): ''' render the map see http://www.pygal.org/en/stable/documentation/output.html ''' if filename is None: self.wmap.render_in_browser() else: if filename.endswith(".png"): self.wmap.render_to_png(filename) else: self.wmap.render_to_file(filename)
'gh': 24392000, 'gn': 9982000, 'gw': 1515000, 'ci': 19738000, 'lr': 3994000, 'ml': 15370000, 'mr': 3460000, 'ne': 15512000, 'ng': 158423000, 'sn': 12434000, 'sl': 5867000, 'tg': 6028000 } cc_pops_1, cc_pops_2, cc_pops_3 = {}, {}, {} for pop in population.values(): if pop < 10000000: cc_pops_1[pop] = pop elif pop < 90000000: cc_pops_2[pop] = pop else: cc_pops_3[pop] = pop print(len(cc_pops_1), len(cc_pops_2), len(cc_pops_3)) wm.add('Africa', population) wm.add('0-10m', cc_pops_1) wm.add('10-90m', cc_pops_2) wm.add('>90m', cc_pops_3) wm.render_to_file('africa.svg')
for pop_dict in pop_data: #print(pop_dict) if int(pop_dict['Year']) == 2010: country_name = pop_dict['Country Name'] population = int(pop_dict['Value']) code = get_country_code(country_name) if code: cc_populations[code] = population #print(code + ": " + str(population)) else: print('ERROR - ' + country_name) #根据人口数量将所有的国家分成三组 cc_pops_1,cc_pops_2,cc_pops_3 = {},{},{} for cc, pop in cc_populations.items(): if pop < 100000000: cc_pops_1[cc] = pop elif pop < 100000000: cc_pops_2[cc] = pop else: cc_pops_3[cc] = pop #看看每组包含了多少个国家 wm = World() print(len(cc_pops_1),len(cc_pops_2),len(cc_pops_3)) wm_style = RS('#336699', base_style=NS) wm = 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')
else: print('ERROR - ' + country_name) # 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 < 100000000: 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 = World() wm_style = RotateStyle('#336699', base_style=LightColorizedStyle) wm = World(style=wm_style) wm.title = 'World Population in 2010, by Country' # wm.add('2010', cc_populations) 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')
# See how many countries are in each level. print(len(cc_pops_1), len(cc_pops_2), len(cc_pops_3)) """ Essa saída mostra que há 85 países com menos de 10 milhões de pessoas, 69 países com população entre 10 milhões e 1 bilhão de pessoas e dois países extremos com mais de 1 bilhão. """ from pygal.style import LightColorizedStyle as LG dc = {'ca'} wm_style2 = LG wm_style = RS('#336699', base_style=LCS) #wm = pygal.Worldmap(style=wm_style) #NÃO FUNCIONA MAIS ATUALMENTE. #wm = wm(style=wm_style) #GAMBIARRA QUE EU FIZ, MAS TAMBÉM NÃO SURTIU O EFEITO ESPERADO. wm = WD(style=wm_style2) #wm.force_uri_protocol = 'http' wm.title = 'World Population in 2010, by Country' #Aqui Iluminou a America Do Norte toda. O problema é que; estes .adds NÃO estão recebendo Estes dicionarios 'cc_pops_2' #wm.add('0 a 10', ['ca', 'mx', 'us']) wm.add('0-10m', cc_pops_1) #Se eu colocar a Var 'dc = {'ca'}' .Funciona. Mas cc_pops NÃO. wm.add('10m-1bn', cc_pops_2) wm.add('>1bn', cc_pops_3) wm.render_to_file('Pagina_519_20_População Mundial.svg') """ Não Obtive os mesmos Resultados dos mostrados no Livro, pois, o método de Import mostrado no livro esta ultrapassado. E o metodo atual não obtive a mesma reação. """ #Soluções dos Exercicios: #https://ehmatthes.github.io/pcc/solutions/chapter_16.html
from pygal_maps_world.maps import World world_map = World() world_map.title = 'North,Central,and South America' world_map.add('North America',['ca','mx','us']) world_map.add('Central America',['bz','cr','gt','hn','ni','pa','sv']) world_map.add('South America',['ar','bo','br','cl','co','ec','gf','gy','pe','py','sr','uy','ve']) world_map.render_to_file('world_map.svg') #python2 #使用pygal.Worldmap()对象生成地图 #python3 #使用下面的对象生成地图 # from pygal_maps_world.maps import World # world_map = World()
# -*- coding: utf-8 -*- """ ------------------------------------------------- File Name: NorthAmericaPopulation Description : Author : Liangz Date: 2018/10/26 ------------------------------------------------- Change Activity: 2018/10/26: ------------------------------------------------- """ __author__ = 'Liangz' from pygal_maps_world.maps import World world_map = World() world_map.title = 'Populations Of Countries In North America' world_map.add('North America', { 'ca': 34126000, 'us': 309349000, 'mx': 113423000 }) world_map.render_to_file('NA_population.svg')
cc_populations = {} for pop_dict in pop_data: if str(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 = RS('#336699', base_style=LCS) wm = 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 json from pygal_maps_world.maps import World from country_codes import get_country_code from pygal.style import LightColorizedStyle as LCS, RotateStyle as RS filename = 'gdp_json.json' with open(filename) as f: pop_data = json.load(f) gdp = {} for gdp_dict in pop_data: if gdp_dict['Year'] == 2010: country_name = gdp_dict['Country Name'] gdp_val = float(gdp_dict['Value']) code = get_country_code(country_name) if code: gdp[code] = gdp_val wm_style = RS('#336699', base_style=LCS) wm = World(style=wm_style) wm.title = "World GDP in 2010, by Country" wm.add("GDP", gdp) wm.render_to_file('world_gdp.svg')
with open(filename) as f: reader = csv.reader(f) for row in reader: if row[2] == '2000': value = int(float(row[3])) country_name = row[0] code = get_country_code(country_name) if code: gdps[code] = value gdp_1, gdp_2, gdp_3 = {}, {},{} for name, gdp in gdps.items(): if gdp < 50000000000: gdp_1[name] = gdp elif gdp < 100000000000: gdp_2[name] = gdp else: gdp_3[name] = gdp wm_style = RotateStyle('#338899', base_style=LightColorizedStyle) wm = World(style=wm_style) wm.title = 'World Gdp in 2000, by Country' wm.add('<50000000000', gdp_1) wm.add('<100000000000', gdp_2) wm.add('>100bn', gdp_3) wm.render_to_file('gdp_2000.svg')
from pygal_maps_world.maps import World wm = World() wm.title = 'Populations of Countries in North America' wm.add('North America', {'ca': 34126000, 'us': 309349000, 'mx': 113423000}) wm.render_to_file('na_population.svg')
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 = World(style=wm_style) wm.title = 'World Population 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')
filename = 'gdp_json.json' with open(filename) as f: gdp_data = json.load(f) cc_gdps = {} for gdp_dict in gdp_data: if gdp_dict['Year'] == 2010: country_name = gdp_dict['Country Name'] gdp = int(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 < 1000000000000: cc_gdps_2[cc] = gdp else: cc_gdps_3[cc] = gdp #绘制世界地图 wm = World() wm.title = 'World GDP in 2010' wm.add('0-10bn', cc_gdps_1) wm.add('10bn-1000bn', cc_gdps_2) wm.add('>1000bn', cc_gdps_3) wm.render_to_file('World_GDP.svg')
# population of per country 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_name(country_name) if code: cc_population[code] = population else: print("Error - " + country_name) # grouping countries by 3 types of pipulations cc_pop_1, cc_pop_2, cc_pop_3 = {}, {}, {} for cc, pop in cc_population.items(): if pop < 10000000: # 10 000 000 cc_pop_1[cc] = pop elif pop < 1000000000: # 1 000 000 000 cc_pop_2[cc] = pop else: cc_pop_3[cc] = pop wm_style = RotateStyle('#336699') wm = World(style=wm_style) wm.title = 'World population in 2010, By countries' wm.add('0-10m', cc_pop_1) wm.add('10m-1bn', cc_pop_2) wm.add('>1bn', cc_pop_3) wm.render_to_file('word_pop_by_groups_wm_style.svg')
code = get_country_code(country_name) if code: tmp = {code: export_value_index} tx_dic.update(tmp) else: print("Błąd - " + country_name) #print(tx_dic) # Podzielenie państw na trzy grupy według wielkości indexu tx_dic_100, tx_dic_300, tx_dic_more = {}, {}, {} for cc, pop in tx_dic.items(): if pop < 100: tx_dic_100[cc] = pop elif pop < 300: tx_dic_300[cc] = pop else: tx_dic_more[cc] = pop # Przygotowanie wykresu wm_style = RS('#336699', base_style=LCS) wm = World(style=wm_style) wm.force_url_protocol = 'http' wm.title = 'Export value index w 2017 roku. (dane dla poszczególnych państw)' wm.add('< 100', tx_dic_100) wm.add('100 < 300', tx_dic_300) wm.add('< 300', tx_dic_more) wm.render_to_file('Export_value_index_2017.svg')
cc_populations = {} for pop_dict in pop_data: if pop_dict['Year'] == 2015: country_name = pop_dict['Country Name'] # 有些值是小数,先转为float再转为int population = int(float(pop_dict['Value'])) code = get_country_code(country_name) if code: cc_populations[code] = population # 为了使颜色分层更加明显 cc_populations_1, cc_populations_2, cc_populations_3 = {}, {}, {} for cc, population in cc_populations.items(): if population < 10000000: cc_populations_1[cc] = population elif population < 1000000000: cc_populations_2[cc] = population else: cc_populations_3[cc] = population wm_style = RotateStyle('#336699', base_style=LightColorizedStyle) world = World(style=wm_style) world.title = 'World Populations in 2015, By Country' world.add('0-10m', cc_populations_1) world.add('10m-1bn', cc_populations_2) world.add('>1bn', cc_populations_3) world.render_to_file('world_population_2015.svg') # https://www.jianshu.com/p/6fcd47b3528b
population = int(float(pop_dict['Value'])) code = get_country_code(country_name) if code: # print(code + ':' + str(population)) whole_population[code] = population # else: # print('Error - ' + country_name) # 根据人口数量将所有的国家分成三组 world_population_1, world_population_2, world_population_3 = {}, {}, {} for world_code, population in whole_population.items(): if population < 10000000: world_population_1[world_code] = population elif population < 1000000000: world_population_2[world_code] = population else: world_population_3[world_code] = population # 看看每组分别包含多少个国家 print(len(world_population_1), len(world_population_2), len(world_population_3)) world_map_style = RotateStyle('#336699', base_style=LightColorizedStyle) # world_map_style = LightColorizedStyle world_map = World(style=world_map_style) world_map.title = 'World Population In 2010, By Country' world_map.add('0 - 10M', world_population_1) world_map.add('10M - 1BN', world_population_2) world_map.add('>1BN', world_population_3) world_map.render_to_file('threelevel_light_style.svg')
import pandas as pd from pygal.style import * from pygal_maps_world.maps import World from country_codes import get_country_code csv = pd.read_csv("co2_emissions.csv", keep_default_na=False, skiprows=4) csv['code'] = csv['Country Name'].apply(get_country_code) # 需要把CO2排放量转换为数字格式 csv['digit_2013'] = pd.to_numeric(csv['2013']) # 把CO2排放量分3个层次 # 并把国家码和对应的2013年CO2排放量取出来放在字典中 co2_1, co2_2, co2_3 = {}, {}, {} for code, co2 in zip(csv['code'], csv['digit_2013']): if co2 <= 5: co2_1[code] = co2 elif co2 <= 10: co2_2[code] = co2 else: co2_3[code] = co2 wm = World(fill=True, style=RedBlueStyle) wm.title = "World's CO2 emissions at 2013" wm.add("CO2 <= 5", co2_1) wm.add("CO2 <= 10", co2_2) wm.add("CO2 > 10", co2_3) wm.render_to_file("practice_16-7.svg")
from pygal_maps_world.maps import World wm = World() wm.title = 'Americas' 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')
cc_population = {} # population of per country 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_name(country_name) if code: cc_population[code] = population else: print("Error - " + country_name) # grouping countries by 3 types of pipulations cc_pop_1, cc_pop_2, cc_pop_3 = {}, {}, {} for cc, pop in cc_population.items(): if pop < 10000000: # 10 000 000 cc_pop_1[cc] = pop elif pop < 1000000000 : # 1 000 000 000 cc_pop_2[cc] = pop else: cc_pop_3[cc] = pop wm = World() wm.title = 'World population in 2010, By countries' wm.add('0-10m', cc_pop_1) wm.add('10m-1bn', cc_pop_2) wm.add('>1bn', cc_pop_3) wm.render_to_file('word_pop_by_groups.svg')
# 为了使颜色分层更加明显 cc_level_1, cc_level_2, cc_level_3,cc_level_4,cc_level_5,cc_level_6,cc_level_7 = {}, {}, {},{}, {}, {},{} for cc, number1 in cc_movie_number.items(): if number1 > 3000: cc_level_7[cc] = number1 elif number1 < 3000 and number1 > 2000: cc_level_6[cc] = number1 elif number1 < 2000 and number1 > 900: cc_level_5[cc] = number1 elif number1 < 900 and number1 > 675: cc_level_4[cc] = number1 elif number1 < 675 and number1 > 450: cc_level_3[cc] = number1 elif number1 < 450 and number1 > 225: cc_level_2[cc] = number1 else: cc_level_1[cc] = number1 wm_style = RotateStyle(color='#fde0dc', base_style=LightColorizedStyle) world = World(style=wm_style) world.title = '近10年世界范围各个地区的电影数量' world.add('不足225部', cc_level_1) world.add('225——450部', cc_level_2) world.add('450——675部', cc_level_3) world.add('675——900部', cc_level_4) world.add('900——2000部', cc_level_5) world.add('2000——3000部', cc_level_6) world.add('超过3000部', cc_level_7) world.render_to_file('近10年世界范围各个地区的电影数量.svg')
from pygal_maps_world.maps import World wm = 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')
f = open("gdp.json") gdp = json.load(f) gdps = {} # 把所有国家的名称通过get_country_code函数以得到其国别码 # 并把其GDP存入字典 for item in gdp: if item["Year"] == "2016": code = get_country_code(item["Country Name"]) if code: gdps[code] = int(float(item["Value"])) / 100000000 # 根据GDP数量级别不同把国家分成三组 gdp_1, gdp_2, gdp_3 = {}, {}, {} for c, g in gdps.items(): if g < 1000: gdp_1[c] = g elif g < 10000: gdp_2[c] = g else: gdp_3[c] = g wm_style = RotateStyle('#336699', base_style=LightColorizedStyle) wm = World(style=wm_style) wm.title = "GDP of the Whold World - 2016" wm.add("0-100bn", gdp_1) wm.add("100-1000bn", gdp_2) wm.add("1000-10000bn", gdp_3) wm.render_to_file("gdps.svg")
from pygal_maps_world.maps import World wm = 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')