Hide colorbar from px.choropleth

Hi everyone!

I’m developing an app with dash and I’m producing a lot of graphs, and some of them are choropleth but I’m having a lot of trouble trying to hide the colorbar next to the figure (see picture) , I tried showscale = False and other things but nothing seems to work… Is there any way to solve my problem?

Thanks in advance!

Hi @Cloemdz
Did you try showlegend=None ?

Hi,
Yes I already tried but got the following error message: TypeError: choropleth() got an unexpected keyword argument ‘showlegend’
Do I need to put showlegend=None somewhere else?

@Cloemdz

For any trace type that has the colorscale property, showscale=True is the default setting, and showscale=False, removes the colorbar (it is not displayed).

@empet
I tried:

fig.update_traces(showscale=False)

But it’s still there. I’m using plotly express

@adamschroeder

We have two posibilites to set colorscale:

trace= go.Choroplethmapbox(z=z,
                            locations=locations,
                            colorscale='Viridis',
                            colorbar=dict(thickness=20, ticklen=3),
                            geojson=jdata,
                            hoverinfo='all',
                            marker_line_width=0.1, marker_opacity=0.7)

In this case the showscale is set either via trace.update or fig.data[corresponding_index].update(showscale=False),
or fig.update_traces(showscale=False), when len(fig.data)=1.

  1. coloscale/colorbar are set in layout.coloraxis
trace= go.Choroplethmapbox(z=z,
                            locations=locations,
                            #colorscale='Viridis',
                            #colorbar=dict(thickness=20, ticklen=3),
                            coloraxis='coloraxis',
                            geojson=jdata,
                            hoverinfo='all',
                            marker_line_width=0.1, marker_opacity=0.7)
layout=go.Layout(width=700, height=600,
                coloraxis=dict(colorscale=mapbox,
                               colorbar=dict(thickness=20, ticklen=3))   

When the go.Figureclass was instantied
we perform this update to remove the colorscale:

fig.update_layout(coloraxis_showscale=False)
3 Likes

thank you for the detailed explanation @empet . It worked like a charm. I just corrected the spelling, taking one “s” off.

fig.update_layout(coloraxis_showscale=False)
3 Likes

Thank you!!! :smile:

Hi @empet and @adamschroeder! Any idea how to shift the colorscale to the horizontal (so still show it but not on the vertical like in the above image)? I’ve tried a few combinations but without success so far… I imagine it’s a small variation of the above…

Is this what you’re looking for?

fig.update_layout(legend_orientation="h")

It must be something close to this…! But the colorbar is still displayed vertically when I tried it (also using plotly express).

fig = px.choropleth_mapbox(df, geojson=regions, locations=region_code, color=measure,
                            color_continuous_scale=style[0],
                            range_color=style[1],
                            mapbox_style=style[2],
                            # Automatic centering
                            zoom=get_zoom(regions), 
                            center=get_coords(regions),
                            opacity=0.5,
                            )
fig.update_layout(legend_orientation="h")

@max2

Legends can be placed horizontally: https://plotly.com/python/legend/#horizontal-legend
but the colorbar can be displayed only vertically. You can control its position, length, thickness, by updating fig.layout like this:

fig.update_layout(coloraxis=dict(colorbar_x=1.1, 
                                 colorbar_y=0, 
                                 colorbar_len=0.75, 
                                 colorbar_thickness=20))

For more information print/display:

help(go.layout.coloraxis)

Setting x-position as x=-0.05, your colorbar is placed at the left side of your plot.

2 Likes

Alright, thanks for the comprehensive follow-up!

fig.update_coloraxes(showscale=False)

This is the exact solution.

You can check it

import plotly.express as px
fig = px.density_mapbox(lat=[37.2303], lon=[28.0908], radius=8,
                        center=dict(lat=37.2303, lon=28.0908), zoom=8,
                        mapbox_style="stamen-terrain")
fig.update_coloraxes(showscale=False)
fig.show()
1 Like

Works for me! Thanks!!