繁体   English   中英

如何设置热图纵横比

[英]How to set heatmap aspect ratio

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

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

  • 它真的很慢。 如果我使我的 numpy 阵列大小为 (100,100),则加载需要几分钟。 我在想可能是因为我的代码效率不高?
  • 我不知道如何保持纵横比。 因此,如果我的图像大小为 (100,100) numpy 数组,我希望 plot 也为 (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')

这是它目前的样子:

在此处输入图像描述

此外,如果 plotly 热图不是 go 的最佳方式,我很乐意听到任何替代方案!

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

我不确定这里的每个细节是否都正确,但下面代码片段中的代码将在 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()

速度:

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

很老了,但对于其他任何关注热图性能问题(特别是带注释的热图替代方案)的人来说,这些可能是相关的:

Also, if you are using plotly.express.imshow to plot the heatmap there is an argument to that function aspect='auto' that will update the aspect ratio to fill the space that the plot has.

例如:

import plotly.express as px

# fill/load df accordingly to your needs

fig = px.imshow(df, aspect='auto')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM