Search code examples
pythonplotlyplotly-python

How to change titles (facet_col )in imshow (plotly)


I want to plot several images with imshow from plotly and give each image a title.

My code is

fig = px.imshow(numpy.array(img1,img2), color_continuous_scale='gray', facet_col=0, labels={'facet_col' : 'status'})
fig.update_xaxes(showticklabels=False).update_yaxes(showticklabels=False)
fig.show()

and the result looks like

enter image description here

However, I would like to replace status=0 with original and status=1 with clean. Is there an easy way to achieve this result?

Thanks for any help.


Solution

  • I solved my problem by

    fig = px.imshow(
      numpy.array(img1,img2), 
      color_continuous_scale='gray', 
      facet_col=0
    )
    
    fig.update_xaxes(showticklabels=False).update_yaxes(showticklabels=False)
    
    for i, label in enumerate(['orignal', 'clean']):
        fig.layout.annotations[i]['text'] = label
    
    fig.show()
    

    It would be nice, if there would be a shorter way e.g. passing the list of labels directly to the imshow command. However, I did not find any possibility to do that.