I'm trying to plot a map for africa so the final result looks like the following:
Which is each country colored and have a word / value over it.
So, I tried to use choropleth
from plotly.express
and that's my code:
CSV file:
country capital longitude latitude
Nigeria Bamako -8 12.63
Mali Abuja 7.53 8.93
import geopandas as gpd
import plotly.express as px
import pandas as pd
df = pd.read_csv(dirname)
df_geo = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.longitude, df.latitude))
world_data = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
f = px.choropleth(df, locationmode='country names', locations=df['country'],scope='africa', color=df['country'])
f.show()
and the final result looks like this:
The colors working good but any idea how I can put some values or a word over the colored countries?
Since you didn't provide the sample data from your csv, assuming it's the data you like to use to annotate the maps. In this example I use the country short names as the text which can be added by plotly.go.scattergeo
.
import geopandas as gpd
import plotly.express as px
import json
world_data = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
africa = world_data[world_data.continent=='Africa']
africa_json = json.loads(africa.to_json())
f = px.choropleth(africa,
geojson=africa_json,
featureidkey='properties.name',
locations='name',
color='name')
## Add labels on countries
f.add_scattergeo(
geojson=africa_json,
locations=africa['name'],
featureidkey='properties.name',
text=africa['iso_a3'],
mode='text',
)
f.update_geos(fitbounds='locations')