簡體   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