🚀 Gen 5 of the leading AI app deployment platform launches October 6. Click for the livestream.

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?