Annotations on plotly Choropleth

I am trying to add static labels to a choropleth chart. However, I did not find any information as to how to do that. I would like to display the values (right now all 0 obviously) number statically on the map.

I am currently building my chart like this:

import plotly.express as px
import pandas as pd
import json
from urllib.request import urlopen


def main():
    URL = "https://raw.githubusercontent.com/isellsoap/deutschlandGeoJSON/master/4_kreise/4_niedrig.geojson"
    with urlopen(URL) as response:
        counties = json.load(response)

    df = pd.DataFrame(data=[feat['properties'].get('NAME_3') for feat in counties['features']],columns=['names'])
    df['values'] = 0

    fig = px.choropleth(
        data_frame=df,
        geojson=counties,
        color="values",
        color_continuous_scale='blues',
        locations="names",
        featureidkey="properties.NAME_3",
        projection="mercator"
    )

    fig.update_geos(fitbounds="locations", visible=False)
    fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})

    fig.show()

if __name__ == '__main__':
    main()

Hi @fbosler,

You can display the values by adding a scattergeo trace :

fig.add_scattergeo(
  geojson=counties,
  locations = df['names'],
  text = df['values'],
  featureidkey="properties.NAME_3",
  mode = 'text') 

Also here a link to a discussion about a similar problem:

hope this helps

Alex -

amazing! Thank you Alex, is there a way to customize the labels (i.e. size and color). I am also more than happy to read up on it in the documentation, but I couldn’t really find anything, however, I might be looking in the wrong locations.

Yes you can set the textfont property of the scattergeo trace:

textfont=dict(
        family="sans serif",
        size=8,
        color="Red"
    )
1 Like

Amazing, you’re the best! Thx.

One more follow-up if you don’t mind. Is it also possible to add an animation_frame to the second trace?

I’m following this question too.

When using an animation frame, the text of all animation frames will be plotted together.

Is there a way to combine the use of animation frame and scatter geo?

I followed the advice of using


fig.add_scattergeo(
  geojson=counties,
  locations = df['names'],
  text = df['values'],
  featureidkey="properties.NAME_3",
  mode = 'text')

and I get the figure just as before. There is no error, it gets plotted, but there is no static annotation being displayed. I thought maybe I should somehow update the figure, so I added the lines

    fig.update_geos(fitbounds="locations", visible=False)
    fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})

and still no change. What am I missing?
Here is my full code:

fig = px.choropleth_mapbox(df_choro, geojson=geojson,color=df_choro['Презастрояване'],
                           locations=df_choro.index, featureidkey="properties.Name",
                           center={"lat": 42.694865, "lon": 23.320813},
                           mapbox_style="carto-positron", zoom=10)

fig.add_scattergeo(
  geojson=geojson,
  locations = df_choro.index,
  text = df_choro['Презастрояване'],
  featureidkey="properties.Name",
  mode = 'text') 

fig.update_geos(fitbounds="locations", visible=False)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})

fig.show()

and here is what I am getting:
Screenshot from 2022-06-10 18-46-37

@NeStack
As long as you defined a choroplethmapbox, you have to add annotations to your choropleth via go.Scattermapbox, NOT go.Scattergeo.

An example of scattermapbox that defines annotations:

import plotly.graph_objects as go
mapboxt = open(".mapbox_token").read().rstrip() #my mapbox_access_token 
lats = [52.370216, 53.2191696, 52.160114,  50.851368, 51.8125626]
lons = [4.895168,  6.5666699, 4.497010, 5.690973, 5.8372264 ]
text= ['Amsterdam', 'Groningen', 'Leiden', 'Maastricht', 'Nijmegen']
fig = go.Figure(go.Scattermapbox(lat=lats,
                         lon=lons,
                         mode='text+markers',
                         text=text,   
                         textposition='top center',
                         marker_size=12, marker_color='red'))
fig.update_layout(title_text ='Netherlands', title_x =0.5, width=750, height=700,
                   mapbox = dict(center= dict(lat=52.370216, lon=4.895168),            
                                 accesstoken= mapboxt,
                                 zoom=6,
                                 style="light" 
                               ))
fig.show()

Following this example, you should set up the lists of lon and lat for the locations on your map, where you want to display some information, as well as the text list,
and the code like this:

fig.add_scattermapbox(....)