问题描述
我有一个单通道图像,其中每个整数像素值都映射到一个字符串.例如5->'person'.我正在尝试创建一个交互式图像,将鼠标悬停在一个像素上将显示其对应的字符串.
I have a single-channel image where each integer pixel value maps to a string. For example 5 -> 'person'. I'm trying to create an interactive image where hovering over a pixel will display it's corresponding string.
我认为使用散点图可能是这样做的方法.我遇到的问题是:
I figured using plotly heatmaps might be the way to do this. The issues I'm having are:
- 这真的很慢.如果我将numpy数组的大小设置为偶数(100,100),则需要几分钟的时间来加载.我在想这可能是因为我的代码效率不高吗?
- 我不知道如何保持宽高比.因此,如果我的图像是一个大小为(100,100)的numpy数组,我希望绘图也为(100,100)像素.
- 为
z_text
使用空白值似乎是一个错误的解决方法,但是设置annotation_text = None
似乎无效.
- It's really slow. If I make my numpy array even (100,100) size it takes minutes to load. I'm thinking it might be because my code is not efficient?
- I can't figure out how to maintain aspect ratio. So if my image is a size (100,100) numpy array, I'd like the plot to also be (100,100) pixels.
- using blank values for
z_text
seems like a bad workaround, but settingannotation_text=None
doesn't seem to work.
有人可以在这里帮助我吗?这就是我所拥有的:
Can anyone help me out here? Here's what I've got:
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')
这是当前的样子:
此外,如果绘制热图不是解决此问题的最佳方法,我也想听听其他选择!
Also if a plotly heatmap is not the best way to go about this I'd love to hear any alternatives!
注意:我当前正在jupyterlab内部显示.
Note: I'm currently displaying inside jupyterlab.
推荐答案
我不确定这里是否所有细节都正确,但是下面代码段中的代码将在Jupyter Notebook中生成以下图表.处理长宽比的行是:
I'm not sure if I've gotten every detail correct here, but the code in the snippet below will produce the following plot in a Jupyter Notebook. The line that handles the aspect ratio is:
fig['layout']['yaxis']['scaleanchor']='x'
您还可以使用:
fig.update_layout(yaxis = dict(scaleanchor = 'x'))
情节1:
图2:
只需确保包含以下内容:
Just make sure to include:
fig.update_layout(plot_bgcolor='rgba(0,0,0,0)')
否则,您将得到以下结果:
Or else you'll end up with this:
代码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()
速度:
即使这个小数字也需要一些时间来绘制,但是到目前为止,我对如何加快速度还没有任何建议.
Even this small figure takes some time to plot, but so far I do not have any suggestions on how to speed things up.
这篇关于情节:如何设置热图长宽比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!