【问题标题】:How to change titles (facet_col )in imshow (plotly)如何在 imshow 中更改标题(facet_col)(情节)
【发布时间】:2021-02-04 13:17:38
【问题描述】:

我想用 imshow from plotly 绘制几张图片,并给每张图片一个标题。

我的代码是

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()

结果看起来像

但是,我想将status=0 替换为original,将status=1 替换为clean。有没有简单的方法来达到这个结果?

感谢您的帮助。

【问题讨论】:

    标签: python plotly plotly-python


    【解决方案1】:

    我解决了我的问题

    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()
    

    如果有更短的方式,例如将标签列表直接传递给imshow 命令。但是,我没有找到任何可能这样做。

    【讨论】:

    • 我知道这个问题已经解决了,但我想提出另一种可能更简单的解决方法:fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))source
    【解决方案2】:

    根据文档,添加参数“x”和名称列表应该可以解决它。

    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()
    

    【讨论】:

    • x选项抛出ValueError:x向量的长度必须与img矩阵的第二维长度匹配。 x 中的标签似乎想要沿第二维标记每个像素。哪个不是标题。不过谢谢
    猜你喜欢
    • 1970-01-01
    • 2022-06-11
    • 2021-04-22
    • 2015-06-07
    • 2020-05-12
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多