How to hide the colorbar and legend in plotly express bar graph?
Asked Answered
M

7

30

I am creating a bar graph with something like this:

px.bar(df, x=x, y=y, color=c,
       title=params['title'], 
       hover_data=hover_data)

When c != None the graph produces either a legend or a colorbar. How can I remove either of them?

Martlet answered 30/10, 2019 at 17:37 Comment(0)
T
30

For the legend, you can use either fig.update_traces(showlegend=False) or fig.update(layout_showlegend=False).

For the colorbar, you can only use fig.update(layout_coloraxis_showscale=False). On normal plotly figures, you could also do fig.update_traces(marker_showscale=False), but it does not work for plotly express because of how facetted traces need to share the same colorscale .

Twinned answered 2/11, 2019 at 10:11 Comment(4)
fig.update_traces(marker_showscale=False) does work for me in Plotly express (I'm on using Plotly version 3.10.0)Stuart
Thanks @PolinaSklyarevsky, I have only been using plotly since v4 so I am not sure how it behaved before that.Twinned
In Plotly 5.14.0, fig.update(layout_coloraxis_showscale=False) works but fig.update_traces(marker_showscale=False) does not, at least for my use case.Note
This code can also be written as fig.update_layout(coloraxis_showscale=False).Note
N
28

In v5.1.0, this works for me:

fig.update_coloraxes(showscale=False)
Before After
Bar chart with gradient color bar on right side Only bar chart
Nissen answered 28/7, 2021 at 6:49 Comment(1)
This is just a side note for anyone reaching this answer trying to remove the colorbar for a go.Heatmap, (i.e. using graph_objects, not plotly express). You must first specify a coloraxis when making the Heatmap trace e.g. go.Heatmap(..., coloraxis='coloraxis'). Alternatively, fig.update_traces(showscale=False) will do the trick.Request
H
24

This worked for me!

fig.update_traces(showscale=False)
Hoecake answered 22/6, 2020 at 18:47 Comment(0)
S
13

fig.update_layout(coloraxis_showscale=False) worked for me in plotly 5.6.0

Stefanysteffane answered 21/7, 2022 at 10:5 Comment(0)
C
5

To hide the colorbar with plotly express using Plotly V4:

fig.update_traces(
    marker_coloraxis=None
)
Celibate answered 17/4, 2020 at 14:11 Comment(1)
This removed the continuous color completely ;-;Nissen
W
2

In addition to the options already presented, fig.update_layout(coloraxis_showscale=False) works, too.

I find I often use fit.update_layout() already, so it's nice to be able to get rid of the colorbar in the same method call.

Whiteman answered 30/5, 2022 at 23:44 Comment(0)
C
0

Adding

fig.update_layout(showlegend=False)

worked for me in plotly version 5.15

Constance answered 24/6, 2023 at 0:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.