I am trying to create a choropleth map using plotly and mapbox similar to the US map https://plot.ly/python/mapbox-county-choropleth/ but of African countries. I am new to Python and coding period, so I am a bit lost. My geo data json doesn't have the 'id' field like the the Plotly example. Do I need to create one? Or do I need to just index by df by country? But then how do I match up the instances in the json file? Any help would be appreciated
with open('custom.geo.json') as response:
africa_geo = json.load(response)
data=pd.read_csv('africa_project.csv')
africa_geo['features'][0]
{'type': 'Feature',
'properties': {'scalerank': 0,
'featurecla': 'Admin-0 country',
'labelrank': 3,
'sovereignt': 'Burkina Faso',
'sov_a3': 'BFA',
'adm0_dif': 0,
'level': 2,
'type': 'Sovereign country',
'admin': 'Burkina Faso',
'adm0_a3': 'BFA',
'geou_dif': 0,
'geounit': 'Burkina Faso',
'gu_a3': 'BFA',
'su_dif': 0,
'subunit': 'Burkina Faso',
'su_a3': 'BFA',
'brk_diff': 0,
'name': 'Burkina Faso',
'name_long': 'Burkina Faso',
'brk_a3': 'BFA',
'brk_name': 'Burkina Faso',
'brk_group': None,
'abbrev': 'B.F.',
'postal': 'BF',
'formal_en': 'Burkina Faso',
'formal_fr': None,
'note_adm0': None,
'note_brk': None,
'name_sort': 'Burkina Faso',
'name_alt': None,
'mapcolor7': 2,
'mapcolor8': 1,
'mapcolor9': 5,
'mapcolor13': 11,
'pop_est': 15746232,
'gdp_md_est': 17820,
'pop_year': -99,
'lastcensus': 2006,
'gdp_year': -99,
'economy': '7. Least developed region',
'income_grp': '5. Low income',
'wikipedia': -99,
'fips_10_': 'UV',
'iso_a2': 'BF',
'iso_a3': 'BFA',
'iso_n3': '854',
'un_a3': '854',
'wb_a2': 'BF',
'wb_a3': 'BFA',
'woe_id': 23424978,
'woe_id_eh': 23424978,
'woe_note': 'Exact WOE match as country',
'adm0_a3_is': 'BFA',
'adm0_a3_us': 'BFA',
'adm0_a3_un': -99,
'adm0_a3_wb': -99,
'continent': 'Africa',
'region_un': 'Africa',
'subregion': 'Western Africa',
'region_wb': 'Sub-Saharan Africa',
'name_len': 12,
'long_len': 12,
'abbrev_len': 4,
'tiny': -99,
'homepart': 1,
'filename': 'BFA.geojson'},
'geometry': {'type': 'Polygon',
'coordinates': [[[-0.3976712649999, 15.002134501000072],
[-0.361471923999943, 15.017740784000113],
[-0.29917598499992, 15.054741110000064],
[-0.236699177999867, 15.065618999000035],
[-0.166987670999902, 15.049676819000098],
[-0.033404092999916, 14.995933329000096],
[-0.033197387999962, 14.995933329000096],
[0.218466838000012, 14.910977275000109],
[0.22084395400006, 14.888213806000126],
[0.211852254000121, 14.874803772000078],
...]]}}
fig = go.Figure(go.Choroplethmapbox(geojson=africa_geo, locations=data.country_name, z=data.ranking,
colorscale="Viridis"))
fig.update_layout(mapbox_style="dark", mapbox_accesstoken=token,
mapbox_zoom=1, mapbox_center = {"lat": 6.6111, "lon": 20.9394})
fig.show()
Turns out that key is having the "id", a unique identifier called "id". If your geo.json doesn't have that it will not work. Then you have to associate the countries with the unique id and make sure your 'locations' is set equal to the df.id