4

In Python, I'm plotting a choropleth with some data for some countries in Africa:

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]

Plotting this data like so:

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)

Output is an interactive map with the z value and ISO-3 code displayed when you hover over.

Intended output: I would like to have the country's name displayed rather than ISO-3 code. I suppose this can be done by passing in the countries' names as the locations and setting locationmode to 'country names'.

Is there a mapping from ISO to country name for the purposes of this? A list/dict/DataFrame of corresponding values within the plotly config, for example? I've had a look but can't find anything.

Thank you

Chris Browne
  • 449
  • 5
  • 14
  • Refer this [answer](https://stackoverflow.com/a/56717385/1079086) for details. this is not supported yet as it has fix values. – Akash senta Jun 25 '20 at 01:48
  • Thanks for your answer Akash, but that doesn't really answer my question. What I want to know is how to map the ISO-3 code to a country name that plotly will recognise, if `locationmode` is set to `'country names'`. I guess there is a set list of country names that are valid? Thanks – Chris Browne Jun 25 '20 at 01:58

1 Answers1

2

We converted the country name by referring to a two-letter abbreviation from a three-letter abbreviation. The site from which the data was referenced is the following

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']
r-beginners
  • 31,170
  • 3
  • 14
  • 32