def visual_map(countries_export_in): """ Визуализируем данные на карте """ # визуализируем данные на карте мира world_map = maps.World() world_map.title = "Экспорт товаров и услуг (% от ВВП) за последние года" world_map.add("Страны", countries_export_in) world_map.render_to_file('export_countries.svg') print("Файл карты успешно создан - октройте в браузере.")
def generate_result_file(year): conn = create_connection() with conn: dataset = select_by_year(conn, str(year)) for data in dataset: country_name = data[1] population = data[3] code = get_country_code(country_name) if code: cc_populations[code] = population else: cc_populations["na"] = 0 for data in dataset: country_name = data[1] population = data[3] code = get_country_code(country_name) if code: cc_populations[code] = population else: cc_populations["na"] = 0 cc_pops_1, cc_pops_2, cc_pops_3 = {}, {}, {} for cc, my_pop in cc_populations.items(): pop = int(float(my_pop)) if pop < 10000000: cc_pops_1[cc] = pop elif pop < 1000000000: cc_pops_2[cc] = pop else: cc_pops_3[cc] = pop wm_style = RotateStyle('#336699', base_style=LightColorizedStyle) wm = maps.World(style=wm_style) wm.title = 'World populations in ' + str(year) + ', by country' wm.add('0 - 10m - ' + str(len(cc_pops_1)), cc_pops_1) wm.add('10m - 1bn - ' + str(len(cc_pops_2)), cc_pops_2) wm.add('>1bn - ' + str(len(cc_pops_3)), cc_pops_3) return wm.render_to_file('visualize/static/visualize/map.svg')
population = int(float(pop_dict["Value"])) #Some data are floats; code = get_country_code(country) if (code): cc_populations[code] = population else: print("ERROR - " + country) cc_pops_1, cc_pops_2, cc_pops_3 = {}, {}, {} for cc, pops in cc_populations.items(): if (pops < 10000000): cc_pops_1[cc] = pops elif (pops < 1000000000): cc_pops_2[cc] = pops else: cc_pops_3[cc] = pops wm_style = RS("#336699", base_style=LCS) #wm_style = LightColorizedStyle; #wm_style = RotateStyle("#330000");#RGB wm = MP.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) #Darker color for larger number and vice versa; wm.render_to_file("world_population.svg")
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 == None: print(country_name) #打印每个国家2010年的人口数量 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 #这样就能直接添加进字典不需要append()方法,因为字典无序 # else: #这块没必要留着了 # print("Error - "+country_name) #print(country_name+': '+str(population)) wm = pm.World() wm.title = 'World Population in 2010,by Country' # wm.add('North America', {'ca': 34126000, 'us': 309349000, 'mx': 113423000}) # 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.add('2010', cc_populations) wm.render_to_file('world_population.svg')
import json from pygal_maps_world.i18n import COUNTRIES def get_country_code(country_name): """Return the Pygal 2-digit country code for the given country.""" for code, name in COUNTRIES.items(): if name == country_name: return code # If the country wasn't found, return None. return None with open('C:/Users/孙林轩/Desktop/gdp_json.json') as gdpF: data=json.load(gdpF) indexnum=[] for i in range(0,len(data)): if data[i]['Year']==2010: indexnum.append(i) wm=pmw.World() wm.title = 'World map' GDP_dic={} for i in indexnum: GDP_dic[get_country_code(data[i]["Country Name"])]=data[i]['Value'] GDP1={} GDP2={} GDP3={} for code,value in GDP_dic.items(): if value<(1/3)*max(GDP_dic.values()): GDP1[code]=value elif value<(2/3)*max(GDP_dic.values()): GDP2[code]=value else: GDP3[code]=value wm.add('2010 GDP under 1/3',GDP1)
from pygal_maps_world import maps from pygal_maps_world.i18n import COUNTRIES worldmap_chart=maps.World() worldmap_chart.title='C Countries' ccodes=[] for each in COUNTRIES: if each.startswith('c')==True: ccodes.append(each) worldmap_chart.add('C Countries',ccodes) worldmap_chart.render_to_file('map.svg')
# encoding= utf-8 # @Time : 2020/5/8 9:10 # @Author : Yao # @File : americas.py # @Software: PyCharm import pygal import pygal_maps_world.maps as pwm wm = pwm.World() wm.title = 'North, Central, and the America' wm.add('North America', ['ca', 'mx', 'us']) wm.add('Central America', ['bz', 'zr', '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')
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_pop_1, cc_pop_2, cc_pop_3 = {}, {}, {} for cc, pop in cc_populations.items(): if pop < 10000000: cc_pop_1[cc] = pop elif pop < 1000000000: cc_pop_2[cc] = pop else: cc_pop_3[cc] = pop wm = wm.World() wm.title = 'World Population in 2010' 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_pop.svg')
import pygal_maps_world.maps as pyg wm = pyg.World() wm.title = 'Population of Countries in North America' wm.add('North America', {'ca': 34126000, 'mx': 113423000, 'us': 309349000}) # render_to_file() 创建一个包含该图表的.svg文件 wm.render_to_file('num_population.svg')
# выводим население каждой страны за 2018 год cc_population = {} for pop_dict in pop_data: if pop_dict['Year'] == 2018: country_name = pop_dict["Country Name"] population = int(pop_dict['Value']) code = get_country_code(country_name) if code: cc_population[code] = population else: print(f"{country_name}") # сгруппируем страны по уровням населения cc_pops_1, cc_pops_2, cc_pops_3, = {}, {}, {} for cc, pop in cc_population.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 = style.RotateStyle('#336699') world_map = maps.World(style=wm_style) world_map.title = "Численность населения стран мира в 2018 году" world_map.add("0-10m", cc_pops_1) world_map.add("10m-1b", cc_pops_2) world_map.add(">1b", cc_pops_3) world_map.render_to_file('world_population.svg')
import pygal_maps_world.maps as WM wm = 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')
import pygal_maps_world.maps as MP wm = MP.World() wm.title = "North, Central, and Sounth America" wm.add("North America", ["ca", "mx", "us"]) #Each new add will change a different color; 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")
code = get_country_code(country) 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 = RotateStyle('#336699') #改变地图颜色 wm = pymap.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') # #打印每个国家2010年的人口数量 # for pop_dict in pop_data: # if pop_dict['Year']=='2010': # country=pop_dict['Country Name'] # population=int(float(pop_dict['Value'])) #'{:,}'.format(int(pop_dict['Value'])) # pop_dict['Value'] # code=get_country_code(country) # if code: # print(code + ': ' + str(population)) # else:
cc_pops_1, cc_pops_2, cc_pops_3 = {}, {}, {} for cc, pop in cc_population.items(): if pop < 10000000: cc_pops_1[cc] = pop elif pop < 1000000000: cc_pops_2[cc] = pop else: cc_pops_3[cc] = pop print('人口小于10000000国家个数为: ', len(cc_pops_1)) print('人口大于等于10000000小于1000000000国家个数为: ', len(cc_pops_2)) print('人口大于等于1000000000国家个数为: ', len(cc_pops_3)) wm_style = RotateStyle('#336699', base_style=LightColorizedStyle) # 为世界地图指定一种基色 wm = pywm.World(style=wm_style) # 创建世界地图 # wm.title = '北美、中美和南美' # 给图表设置属性 # wm.add('北美', ['ca', 'mx', 'us']) # 添加需要突出显示的国家和国别码 # wm.add('北美', {'ca': 34126000, 'us': 309349000, 'mx': 113423000}) # 不仅显示地区, 还显示地方对应的人口, 传递字典类型参数而非列表 # wm.add('中美', ['bz', 'cr', 'gt', 'hn', 'ni', 'pa', 'sv']) # wm.add('南美', ['ar', 'bo', 'br', 'cl', 'co', 'ec', 'gf', # 'gy', 'pe', 'py', 'sr', 'uy', 've']) wm.title = '2010年世界人口地图' # wm.add('2010', cc_population) # 直接将一个字典传入到图形中 wm.add('0-10m', cc_pops_1) wm.add('10m-1bn', cc_pops_2) wm.add('>1nb', cc_pops_3)
# 根据人口数量将所有的国家分为三组 cc_pops1,cc_pops2,cc_pops3={},{},{} for cc,pop in cc_population.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)) #wm=pyg.World() #wm_style=RoteStyle('#336699') wm_style=pygal.style.RotateStyle('#3399AA',base_style=pygal.style.LightColorizedStyle) wm=pyg.World(style=wm_style) wm.title = 'World Population in 2010,by Countries' wm.add('0-10m',cc_pops1) wm.add('10m-1bn',cc_pops2) wm.add('>1bn',cc_pops3) #wm.add('2010', cc_population) # render_to_file() 创建一个包含该图表的.svg文件 wm.render_to_file('world_population.svg') """ 常见错误 File "D:/Python/hoilday_codes/csv/4_json_world_map.py", line 13, in <module> Caribbean small states:6880000 population = int(pop_dict['Value'])
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: print(f"{code}: {population}") else: print(f"ERROR: {country_name}") 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 < 10_000_000: cc_pops_1[cc] = pop elif pop < 1_000_000_000: cc_pops_2[cc] = pop else: cc_pops_3[cc] = pop wm_style = RS('#336699', base_style=LCS) wm = maps.World(style=wm_style) wm.title = "Populations of Countries in North America" 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')
# 转化为浮点格式,并保存两位小数 proportion = round(float(row[60]), 2) except ValueError: proportion = 0 # 将国家代码和人口比例依次写入字典中 proportion_countries[country_code] = proportion # 创建三个字典,将不同比例范围的数值写入三个字典中 dict_1, dict_2, dict_3 = {}, {}, {} for name, value in proportion_countries.items(): if value >= 15: dict_1[name] = value elif value >= 10: dict_2[name] = value else: dict_3[name] = value # 初始化地图,设置颜色风格,添加标题 wm = pmw.World(style=RB) wm.title = 'Proportion of 65+ population (2016)' # 依次添加地图数据 wm.add('large than 15', dict_1) wm.add('between 10 - 15', dict_2) wm.add('smaller than 10', dict_3) # 保存文件 wm.render_to_file('Proportion of 65+ population.svg')
from pygal_maps_world import 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('america.svg')
population = int(float(pop_dict['Value'])) code = get_country_code(country_name) if code: cc_populations[code] = population #这样就能直接添加进字典不需要append()方法,因为字典无序 #根据人口数量将所有国家分成三组 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 < 100000000: 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 =pm.World() wm_style = RotateStyle('#336699', base_style=LightColorizedStyle) wm = pm.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')