Iโm trying to apply annotations on Choropleth map.
Iโve tried solution proposed in this tread:
But still not able to make it work.
Data:
https://raw.githubusercontent.com/softhints/Pandas-Exercises-Projects/main/data/europe_pop.csv
Code
Code 1 - mixed from the forum
import plotly.express as px
import geopandas as gpd
import plotly.graph_objects as go
import pandas as pd
mapboxt = "xxx"
europe = pd.read_csv('https://raw.githubusercontent.com/softhints/Pandas-Exercises-Projects/main/data/europe_pop.csv')
lats = europe['lat']
lons = europe['lon']
text= europe['values'].astype(str)
fig = px.choropleth_mapbox(europe, geojson=europe.geometry, locations=europe.index, color='values',
mapbox_style="carto-positron",
center={"lat": 55, "lon": 15},
zoom=1, opacity=0.5,
hover_data={'values': True},
width=600, height=600)
fig = go.Figure(go.Scattermapbox(lat=lats,
lon=lons,
mode='text+markers',
text=text,
textfont=dict(
family="san serif",
size=15,
color="green"
),
textposition='top center',
marker_size=12, marker_color='red'))
fig.update_layout(title_text ='Europe', title_x =0.5, width=1200, height=1200,
mapbox = dict(center= dict(lat=52.370216, lon=4.895168),
accesstoken= mapboxt,
zoom=6,
style="light"
))
fig.show()
Code 2: choropleth_mapbox + add_annotation
# Plot choropleth map with value labels
fig = px.choropleth_mapbox(europe, geojson=europe.geometry, locations=europe.index, color='values',
mapbox_style="carto-positron",
center={"lat": 55, "lon": 15},
zoom=2,
opacity=0.5,
hover_data={'values': True},
width=1200, height=1200
)
# Add value labels to the map
for idx, row in europe.iterrows():
fig.add_annotation(
x=row['geometry'].centroid.x,
y=row['geometry'].centroid.y,
text=row['name'],
showarrow=True,
font=dict(color='black',
size=10))
fig.update_annotations(xref="x", yref="y")
# Show plot
fig.show()
Expected Result
I would like to plot something similar to:
https://www.reddit.com/r/MapPorn/comments/12daw36/anger_map_of_europe/
Results
I can not get colors and text annotation on a single map:
Annotations are wrongly placed: