Black Lives Matter. Please consider donating to Black Girls Code today.

Plotly express scatter_mapbox hide legend

Hi,

I am trying to the colorbar legends on the right side of my Plotly Express scatter_mapbox plot.

Here’s my code on how to make the plot.

fig = px.scatter_mapbox(df, lat="Latitude", lon="Longitude", color="Data", size="Confirmed", hover_name="State", color_continuous_scale=px.colors.cyclical.IceFire)

I’ve tried:
fig.layout.update(margin={showlegend=False)
fig.update_traces(marker_showscale=False)
fig.layout.showlegend=False
fig.layout.update(showlegend=False)

But the colorbar just does not want to go away. On the Plotly Express scatter mapbox documentation there’s no API to hide the colorbar.

How can I make it go away?

Hi @vitaminc

Type:

fig.layout

to display the layout definition. If you are seeing settings for 'coloraxis', then to remove the colorbar just make the following update:

fig.update_layout(coloraxis_showscale=False)

It’s the same approach as in in the case of chropletapbox discussed here: https://community.plotly.com/t/hide-colorbar-from-px-choropleth/34970/7.

1 Like

That worked! Thank you so very much!!!

@empet

Since I got you here, is there a way to have hover_data not display latitude and longitude? Regardless of which column of data frame I choose, lat/lon always show up.

:frowning:

Thank you so much in advance for your help!

@vitaminc

To remove displaying lon and lat for each point, type:

fig.data[0].hovertemplate

and remove the settings for lon and lat

For this plotly tutorial: https://plot.ly/python/scattermapbox/#basic-example-with-plotly-express

typing fig.data[0].hovertemplate
we get:

'car_hours=%{marker.size}<br>centroid_lat=%{lat}<br>centroid_lon=%{lon}<br>peak_hour=%{marker.color}'

With this update:

fig.data[0].update(hovertemplate= 'car_hours=%{marker.size}<br>peak_hour=%{marker.color}')

lon and lat are no more displayed.

2 Likes

hi guys, Im not sure when this changed, but I attempted to hide the map legend using the method described by @empet fig.update_layout(coloraxis_showscale=False) but it did not work. And just to be sure I did not miss anything I also attempted the method used by @vitaminc fig.layout.update(showlegend=False) and this did work for me. There must have been a recent change.

Im using versions
plotly 4.6.0
plotly-express 0.4.1

Hi @garret

Please tell us what trace type are you defining? What I said about colorbar is still valid. Check it running this code:

import plotly.express as px
px.set_mapbox_access_token(open(".mapbox_token").read())
df = px.data.carshare()
fig = px.scatter_mapbox(df, lat="centroid_lat", lon="centroid_lon",     color="peak_hour", size="car_hours",
                  color_continuous_scale=px.colors.cyclical.IceFire, size_max=15, zoom=10)
fig.show()

Then in a notebook cell:
fig.layout.coloraxis
and you’ll get displayed:

layout.Coloraxis({
    'colorbar': {'title': {'text': 'peak_hour'}},
    'colorscale': [[0.0, '#000000'], [0.0625, '#001f4d'], [0.125, '#003786'],
                   [0.1875, '#0e58a8'], [0.25, '#217eb8'], [0.3125, '#30a4ca'],
                   [0.375, '#54c8df'], [0.4375, '#9be4ef'], [0.5, '#e1e9d1'],
                   [0.5625, '#f3d573'], [0.625, '#e7b000'], [0.6875, '#da8200'],
                   [0.75, '#c65400'], [0.8125, '#ac2301'], [0.875, '#820000'],
                   [0.9375, '#4c0000'], [1.0, '#000000']]
})

Now this update:

fig.update_layout(coloraxis_showscale=False)

removes the colorbar. (I have Plotly 4.6.0, too)

It seems that you are not making distinction between legend and colorbar The colorbar is always associated when you are setting a colorscale, and the showscale=True (its default value is True), and almost sure your comment above is referring to a trace type that doesn’t colormap a list of values to a colorscale.

Hi @empet,

I see now thank you for the clarification.