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
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']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With