4

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.

DerJFK
  • 311
  • 2
  • 15

3 Answers3

3

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.

DerJFK
  • 311
  • 2
  • 15
  • I know this question has already been solved, but I wanted to suggest another way of solving this issue that might be more simple: `fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))` [source](https://plotly.com/python/facet-plots/#controlling-facet-spacing) – Sergi Ballestar Mar 10 '22 at 09:19
2

I appreciate the answer by @DerJFK, however, it won't support multirows.

This trick will fix the problem:

item_map={f'{i}':key for i, key in enumerate(['orignal', 'clean'])}
fig.for_each_annotation(lambda a: a.update(text=item_map[a.text.split("=")[1]])) 
Ali
  • 460
  • 6
  • 13
-1

According to the docs, adding parameter 'x' with a list of names should solve it.

fig = px.imshow(numpy.array(img1,img2), 
    color_continuous_scale='gray', 
    facet_col=0, 
    labels={'facet_col' : 'status'}, 
    x=['Orginal', 'Clean'])
fig.update_xaxes(showticklabels=False).update_yaxes(showticklabels=False)
fig.show()
vcucu
  • 184
  • 3
  • 12
  • 1
    The x options throws ValueError: The length of the x vector must match the length of the second dimension of the img matrix. The labels in x seems to want to label each pixel along the second dimension. Which ist not the title. But thanks – DerJFK Feb 04 '21 at 15:01