Aus*_*tin 5 python heatmap plotly semantic-segmentation
我有一个单通道图像,其中每个整数像素值映射到一个字符串。例如 5 -> '人'。我正在尝试创建一个交互式图像,其中将鼠标悬停在像素上将显示其相应的字符串。
我认为使用绘图热图可能是做到这一点的方法。我遇到的问题是:
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')
Run Code Online (Sandbox Code Playgroud)
这是它目前的样子:
另外,如果情节热图不是解决此问题的最佳方法,我很想听到任何替代方案!
注意:我目前正在 jupyterlab 内显示。
我不确定这里的每个细节是否都正确,但下面代码片段中的代码将在 Jupyter Notebook 中生成以下图。处理纵横比的行是:
fig['layout']['yaxis']['scaleanchor']='x'
Run Code Online (Sandbox Code Playgroud)
您还可以使用:
fig.update_layout(yaxis = dict(scaleanchor = 'x'))
Run Code Online (Sandbox Code Playgroud)
情节 1:
情节 2:
只需确保包括:
fig.update_layout(plot_bgcolor='rgba(0,0,0,0)')
Run Code Online (Sandbox Code Playgroud)
否则你会得到这样的结果:
代码 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)'
Run Code Online (Sandbox Code Playgroud)
代码 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()
Run Code Online (Sandbox Code Playgroud)
速度:
即使是这个小数字也需要一些时间来绘制,但到目前为止,我对如何加快速度没有任何建议。