Custom labels in mapbox

Hi is there a way to insert custom labels in a mapbox i’d would like to put a label above every point i insert in my mapbox with a Scattermapbox element. I tried adding new layers as described in the mapbox tutorial on custom labels but cant make it work.

layers = [
dict(
sourcetype=‘geojson’,
source=s,
type=‘symbol’,
color=‘black’,
opacity=0.1,
name=‘test’
) for s in layers_files]

This is how i tried with a layers it does give me a point woth a circle figure but cant modify it to display text
Any ideas??

@lahashh

go.Scattermapbox trace has hovertext as attribute.

hovertext sets the text to be displayed on hover over each (lon,lat) pair.
If it is given as a single string, the same string appears on over,
over all the data points.
If you define hovertext as a list of strings, they are mapped in order to the
points of (lon,lat) coordinates.
To ensure that these strings are displayed on hover, set `hoverinfo= ‘text’.

thanks for the reply. this displays the text every time y move the cursor above the point of interest I’m looking for static labels display on the mapbox as the city names are display for example

@lahashh

To set static labels, define text instead of hovertext, and mode='text+markers'. See https://plot.ly/python/text-and-annotations/. The same rules for text work in the case of go.Scattermapbox, too.

1 Like

that worked amazingly really appreciate it man thanks a lot

2 Likes

Is there any way do this with plotly express scatter_mapbox? I might be doing too much here, but I’d like to have an animated scatter_mapbox with labels for each of the dots as they move around the map. If I leave out text=, it works fine. If I turn on text=, the animation doesn’t work, (or for some columns, I get a blank figure).

#This makes a map without text labels, obviously, and animation works fine.
fig=px.scatter_mapbox(lat='lat',lon='lon',data_frame=df_teams,mapbox_style='carto-positron',size='Size',
                animation_frame='Timestep_str',color='Team',center={'lon':x_ctr,'lat':y_ctr},zoom=6,
                     labels={'Timestep_str':'Date'},hover_data=['Team'],
                     animation_group='Team')
#This makes a map without text labels or animation.
fig=px.scatter_mapbox(lat='lat',lon='lon',data_frame=df_teams,mapbox_style='carto-positron',size='Size',
                animation_frame='Timestep_str',color='Team',center={'lon':x_ctr,'lat':y_ctr},zoom=6,
                     labels={'Timestep_str':'Date'},hover_data=['Team'],
                     animation_group='Team',
                     text='Team')
#This is a blank map
fig=px.scatter_mapbox(lat='lat',lon='lon',data_frame=df_teams,mapbox_style='carto-positron',size='Size',
                animation_frame='Timestep_str',color='Team',center={'lon':x_ctr,'lat':y_ctr},zoom=6,
                     labels={'Timestep_str':'Date'},hover_data=['Team'],
                     animation_group='Team',
                     text='Progress')

Following up on this for go.scattermapbox. I tried this with both px and go. In both cases, I get the map and the markers. I get hoverinfo on the markers, but no static text shown on the map.

px code:

fig = px.scatter_mapbox(data_frame=pois_df,lon="Longitude", lat="Latitude", text="Name", 
                        size_max=60,mapbox_style='stamen-terrain')

go code:

fig.add_scattermapbox(lon=pois_df.Longitude,lat=pois_df.Latitude,
            mode='markers+text',
            text=pois_df.Name)

print(fig) shows the correct values for lat, lon, mode, text, and type in both cases. If anyone has this working with go.scattermapbox, can you post your code or some suggestions?

Hi @allenite,

The text is not displayed when the mapbox_style='carto-positron' , but withlight, ‘basic’ , etc (i.e. styles that require mapbox accesstoken) it works.:

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"
                                 
                               ))

Got it, I’ll try that. Thanks Empet! Can you explain briefly why it doesn’t work with the other map types, and only with Mapbox token bases?

Thanks again!

@allenite

I don’t know why. Perhaps adding text to markers needs special mapbox resources, provided only with a mapbox accesstoken.

Perfect, I tried it out and it works great. Thanks again, empet!