【问题标题】:Plotly: How to set heatmap aspect ratio?Plotly:如何设置热图纵横比?
【发布时间】:2019-07-27 13:56:22
【问题描述】:

我有一个单通道图像,其中每个整数像素值都映射到一个字符串。例如 5 -> '人'。我正在尝试创建一个交互式图像,将鼠标悬停在一个像素上将显示它的相应字符串。

我认为使用 plotly 热图可能是做到这一点的方法。我遇到的问题是:

  • 真的很慢。如果我将我的 numpy 数组设置为 (100,100) 大小,则加载需要几分钟。我在想这可能是因为我的代码效率不高?
  • 我不知道如何保持纵横比。因此,如果我的图像是一个大小为 (100,100) 的 numpy 数组,我希望绘图也是 (100,100) 像素。
  • z_text 使用空白值似乎是一个糟糕的解决方法,但设置annotation_text=None 似乎不起作用。

有人可以帮我吗?这是我得到的:

import numpy as np
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
import plotly.figure_factory as ff

z = np.random.randint(0,6, size=(10, 10))
z_text = np.full(z.shape, '', dtype=str)

d = {0:'a', 1:'b', 2:'c', 3:'d', 4:'e', 5:'f'}
class_mat = np.vectorize(d.get)(z)

fig = ff.create_annotated_heatmap(z, annotation_text=z_text, text=class_mat, hoverinfo='text', colorscale='Viridis', )
fig.layout.title = 'Semantic Segmentation'

iplot(fig, filename='annotated_heatmap_text')

这是它目前的样子:

此外,如果情节热图不是解决此问题的最佳方式,我很乐意听到任何替代方案!

注意:我目前正在 jupyterlab 中显示。

【问题讨论】:

  • @Austin 我的回答对你有什么影响?
  • 那是不久前的事了,但据我所知,这对于我的用例来说太滞后了。虽然我会选择你的答案,因为它似乎适用于小案例
  • @Austin 只是那个特殊的滞后的产品吗?还是整个文件/笔记本?
  • iirc 只是情节。我们的想法是为至少 100x100 像素的图像执行此操作,因此它不会真正起作用。也许这只是情节的限制,但不太确定。

标签: python plotly heatmap semantic-segmentation


【解决方案1】:

我不确定我是否在这里得到了正确的每个细节,但下面 sn-p 中的代码将在 Jupyter Notebook 中生成以下图。处理纵横比的行是:

fig['layout']['yaxis']['scaleanchor']='x'

你也可以使用:

fig.update_layout(yaxis = dict(scaleanchor = 'x'))

情节 1:

情节 2:

只要确保包括:

fig.update_layout(plot_bgcolor='rgba(0,0,0,0)')

否则你会得到这样的结果:

代码 1 - 我对您的示例所做的修改:

fig.data[0]['hoverinfo'] = 'all'
fig['layout']['yaxis']['scaleanchor']='x'
fig['layout']['xaxis']['gridcolor'] = 'rgba(0, 0, 0, 0)'
fig['layout']['yaxis']['gridcolor'] = 'rgba(0, 0, 0, 0)'
fig['layout']['yaxis']['color'] = 'rgba(0, 0, 0, 0)'

代码 2 - 轻松复制和粘贴的全部内容:

import numpy as np
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
import plotly.figure_factory as ff

#%qtconsole

z = np.random.randint(0,6, size=(10, 10))
z_text = np.full(z.shape, '', dtype=str)

d = {0:'a', 1:'b', 2:'c', 3:'d', 4:'e', 5:'f'}
class_mat = np.vectorize(d.get)(z)

fig = ff.create_annotated_heatmap(z, annotation_text=z_text,
                                  text=class_mat, hoverinfo='text', colorscale='Viridis',
#                                   x = list('ABCDEFGHIJ'),
#                                   y = list('ABCDEFGHIJ')
                                 )
fig.layout.title = 'Semantic Segmentation'

# My suggestions:
fig.data[0]['hoverinfo'] = 'all'
fig['layout']['yaxis']['scaleanchor']='x'

fig['layout']['xaxis']['gridcolor'] = 'rgba(0, 0, 0, 0)'
fig['layout']['yaxis']['gridcolor'] = 'rgba(0, 0, 0, 0)'
fig['layout']['yaxis']['color'] = 'rgba(0, 0, 0, 0)'

fig.update_layout(plot_bgcolor='rgba(0,0,0,0)')

fig.show()

速度:

即使是这个小数字也需要一些时间来绘制,但到目前为止我对如何加快速度没有任何建议。

【讨论】:

    猜你喜欢
    • 2012-05-06
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 2011-12-19
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    • 2020-03-14
    相关资源
    最近更新 更多