如何设置热图长宽比

问题描述 投票:0回答:4

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

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

  • 真的很慢。如果我将 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 内显示。

python plotly heatmap semantic-segmentation
4个回答
12
投票

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

速度:

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


1
投票

此外,如果您使用

plotly.express.imshow
来绘制热图,该函数
aspect='auto'
会有一个参数,它将更新纵横比以填充绘图所具有的空间。

例如:

import plotly.express as px

# fill/load df accordingly to your needs

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

0
投票

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


0
投票

看来这两个答案都不适用于子图: https://stackoverflow.com/a/55239488/2215205 https://stackoverflow.com/a/71579181/2215205

px.imshow(df,aspect ='equal')被fig.add_trace()完全忽略。

在这方面操作图形布局仅适用于第一个子图,而不是全部:

import pandas as pd
import plotly.express as px
import plotly.graph_objects as go

## Example data
df = pd.DataFrame([[0,0,0,0,0],
                   [0,0,1,0,0],
                   [0,1,2,1,0],
                   [0,0,1,0,0],
                   [0,0,0,0,0],
                  ])

## Subplots w/ go.Heatmap()
layout = dict(
    # yaxis=...,
    # xaxis=...,
    # width=plot_width,
    # height=plot_height,
    # autosize=False,
    yaxis_scaleanchor="x" ,  # <---- only works in 1st subplot :(
    )

fig = go.Figure(layout=layout).set_subplots(rows=1, cols=2, subplot_titles=['#1', '#2'])

fig.add_trace(
    go.Heatmap(
        z = df,
        coloraxis = "coloraxis",  # use a common coloraxis for all subplots
    ), 
    1,1)
fig.add_trace(
    go.Heatmap(
        z = df*2,
        coloraxis = "coloraxis",  # use a common coloraxis for all subplots
    ),
    1,2)

## Update y-axes
fig.update_yaxes(
    title_text="yaxis #1", 
    row=1, col=1, 
    scaleanchor='x',  # <---- only works in 1st subplot :(  ==> works in Colab !!
    ) 
fig.update_yaxes(
    title_text="yaxis #2",
    row=1, col=2,
    scaleanchor='x',  # <---- only works in 1st subplot :( ==> works in Colab !!
    )

## Update layout
# fig['layout']['yaxis']['scaleanchor']='x' # <--- updates only first subplot :(

fig.update_layout(
    title='2D Heatmaps',
    autosize=False,
    # yaxis_scaleanchor="x",  # <--- updates only first subplot :(
    yaxis = dict(scaleanchor = 'x'),   # <--- updates only first subplot :(
    # coloraxis_colorscale='Viridis',
    )

# fig.show()

非常感谢任何帮助。

请通过我的努力在这里找到一个Jupyter-Notebook: https://colab.research.google.com/drive/13NKNmgAXudXp62UlmXKDhVFgpFe1YeQN?usp=sharing

© www.soinside.com 2019 - 2024. All rights reserved.