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

Hide colorbar in px.imshow

I’m using px.imshow to display a CT image slice and add_scatter commands to overlay contour lines, and the imshow colorbar and scatter legend end up overlayed. Is there a way to hide the imshow colorbar only? Thanks!

Hi @tks, here is a solution below to disable both the coloraxis and the scale (actually it’s two possible solutions, one using fig.update_traces and the other one manipulating directly the figure). I do a lot of print(fig) in such a case to understand how the figure is built and how I can modify it.

Hope this helps and thanks for your message, the proposed solution is not perfect since you have to give once again the colorscale to use, we’ll try to improve this in the future. Happy image visualization :-).

import as px
import plotly.graph_objects as go
from skimage import data
img =
fig = px.imshow(img, color_continuous_scale='gray')
                       colorscale='gray'), selector={'type':'heatmap'})
# Alternatively, uncomment the following lines (and comment the previous command)[0].showscale=False[0].coloraxis=None[0].colorscale='gray'

fig.add_trace(go.Contour(z=img, showscale=False,
                         contours=dict(start=0, end=70, size=70, coloring='lines'),

Opened for the record.

Thanks @Emmanuelle. Your solution did allow me to remove the colorbar, but also cleared the px.imshow.range_color information (which appears to end up as cmin and cmax within fig.layout.coloraxis) that was controlling my window/level of the image slice. As per the suggestion from @nicolaskruchten in your opened issue below, the following worked for me:

fig.layout.coloraxis.showscale = False

Note that I couldn’t see the showscale property within the layout.coloraxis dict upon print(fig), so I am glad to know it exists!

Yes the solution of @nicolaskruchten works better :-). I’ll add an example to the documentation page (unless you want to do it yourself :-)).

It might be good to sort out another related coloraxis issue before making the documentation change. I had to use range_color because zmin and zmax did not seem to have any effect. Not sure if it matters, but note that ct_img in the example below is a single-channel np.ndarray with dtype=np.int16.

px.imshow(ct_img[:, :, 100].T, zmin=0, zmax=1000) did not work
px.imshow(ct_img[:, :, 100].T, range_color=[0, 1000]) worked as expected

Thanks @tks this is indeed a bug, I opened an issue on