0

我正在尝试使用 pygal.maps.world 模块创建人口地图,但某些国家/地区不会出现在最终文件中。我在有问题的国家/地区附加了代码,但它仍然不起作用。这是获取每个国家/地区对应代码的代码:

from pygal.maps.world 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
        elif country_name == 'Yemen, Rep.':
            return 'ye'
        elif country_name == 'Bolivia, Plurinational State of':
            return 'bo'
        elif country_name == 'United Arab Emirates':
            return 'ae'
        elif country_name == 'Bosnia and Herzegovina':
            return 'ba'
        elif country_name == 'Brunei Darussalam':
            return 'bn'
        elif country_name == 'Congo, the Democratic Republic of the':
            return 'cd'
        elif country_name == 'Central African Republic':
            return 'cf'
        elif country_name == 'Iran, Islamic Republic of':
            return 'ir'
        elif country_name == 'Korea, Democratic People\'s Republic of':
            return 'kp'
        elif country_name == 'Korea, Republic of':
            return 'kr'
        elif country_name == 'Lao People\'s Democratic Republic':
            return 'la'
        elif country_name == 'Lybian Arab Jamahiriya':
            return 'ly'
        elif country_name =='Macedonia, the former Yugoslav Republic of':
            return 'mk'
        elif country_name == 'Moldova, Republic of':
            return 'md'
        elif country_name == 'Palestine, State of':
            return 'ps'
        elif country_name == 'Saint Helena, Ascension and Tristan da Cunha':
            return 'sh'
        elif country_name == 'Taiwan, Province of China':
            return 'tw'
        elif country_name == 'Tanzania, United Republic of':
            return 'tz'
        elif country_name == 'Venezuela, Bolivarian Republic of':
            return 've'
    # If the country wasn't found, return None.
    return None

我设法让也门出现在地图上,这是以前没有的,但似乎该列表没有遍历玻利维亚和其他国家,或者返回的字符串可能没有存储在“代码”中多变的。这是渲染 SVG 文件的代码:

import pygal
from pygal.maps.world import World
from pygal.style import RotateStyle as RS, LightColorizedStyle as LCS
from country_codes import get_country_code
# Load the data into a list.
filename ="population_data.json"
with open(filename) as f:
    pop_data = json.load(f)    
# Build a dictionary of 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 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')

可能是什么问题呢?

4

0 回答 0