plotly:来自 ISO-3 代码的有效国家名称列表

plotly: List of valid country names from ISO-3 code

在 Python 中,我正在为非洲一些国家绘制一个等值线图:

countries = ['BDI', 'BEN', 'BFA', 'BWA', 'CIV', 'CMR', 'COD', 'CPV', 'ETH', 'GHA', 'GIN', 'GMB', 'KEN', 'LBR', 'LSO', 'MDG', 'MLI', 'MOZ', 'MUS', 'MWI', 'NER', 'NGA', 'RWA', 'SEN', 'SLE', 'SOM', 'STP', 'TCD', 'TGO', 'TZA', 'UGA', 'ZAF', 'ZMB', 'ZWE']
z = [5, 6, 1, 1, 2, 14, 7, 1, 3, 6, 1, 2, 13, 1, 3, 11, 4, 2, 1, 6, 1, 50, 18, 5, 2, 4, 1, 1, 4, 16, 15, 4, 10, 4]

像这样绘制此数据:

import plotly.offline as py
import plotly.graph_objs as go

countries = ['BDI', 'BEN', 'BFA', 'BWA', 'CIV', 'CMR', 'COD', 'CPV', 'ETH', 'GHA', 'GIN', 'GMB', 'KEN', 'LBR', 'LSO', 'MDG', 'MLI', 'MOZ', 'MUS', 'MWI', 'NER', 'NGA', 'RWA', 'SEN', 'SLE', 'SOM', 'STP', 'TCD', 'TGO', 'TZA', 'UGA', 'ZAF', 'ZMB', 'ZWE']
z = [5, 6, 1, 1, 2, 14, 7, 1, 3, 6, 1, 2, 13, 1, 3, 11, 4, 2, 1, 6, 1, 50, 18, 5, 2, 4, 1, 1, 4, 16, 15, 4, 10, 4]

layout = dict(geo={'scope': 'africa'})
data = dict(
    type='choropleth',
    locations=countries,
    locationmode='ISO-3',
    colorscale='Viridis',
    z=z)
map = go.Figure(data=[data], layout=layout)
py.plot(map)

输出是一个交互式地图,当您将鼠标悬停在上面时,会显示 z 值和 ISO-3 代码。

预期输出: 我想显示国家名称而不是 ISO-3 代码。我想这可以通过将国家名称作为 locations 并将 locationmode 设置为 'country names'.

来完成

为此,是否存在从 ISO 到国家/地区名称的映射?例如,list/dict/DataFrame 配置中的相应值?我已经看过了,但找不到任何东西。

谢谢

我们通过参考两个字母的缩写从三个字母的缩写转换国家名称。引用数据的站点如下

Country ISO Codes -> Country Names

c_names = []
for c in countries:
    for c2,c3 in iso3.items():
        if c3 == c:
            for v2,v3 in names.items():
                if c2 == v2:
                    c_names.append(v3)

c_names
['Burundi',
 'Benin',
 'Burkina Faso',
 'Botswana',
 'Ivory Coast',
 'Cameroon',
 'Democratic Republic of the Congo',
 'Cape Verde',
 'Ethiopia',
 'Ghana',
 'Guinea',
 'Gambia',
 'Kenya',
 'Liberia',
 'Lesotho',
 'Madagascar',
 'Mali',
 'Mozambique',
 'Mauritius',
 'Malawi',
 'Niger',
 'Nigeria',
 'Rwanda',
 'Senegal',
 'Sierra Leone',
 'Somalia',
 'Sao Tome and Principe',
 'Chad',
 'Togo',
 'Tanzania',
 'Uganda',
 'South Africa',
 'Zambia',
 'Zimbabwe']