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 plotly.express as px
import plotly.graph_objects as go
from skimage import data
img = data.camera()
fig = px.imshow(img, color_continuous_scale='gray')
fig.update_traces(dict(showscale=False, 
                       coloraxis=None, 
                       colorscale='gray'), selector={'type':'heatmap'})
# Alternatively, uncomment the following lines (and comment the previous command)
#fig.data[0].showscale=False
#fig.data[0].coloraxis=None
#fig.data[0].colorscale='gray'

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

fig.show()

Opened https://github.com/plotly/plotly.py/issues/1929 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 https://github.com/plotly/plotly.py-docs/blob/master/python/imshow.md (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 https://github.com/plotly/plotly.py/issues/1931