python - Plotly:如何设置热图纵横比?
问题描述
我有一个单通道图像,其中每个整数像素值映射到一个字符串。例如 5 -> '人'。我正在尝试创建一个交互式图像,将鼠标悬停在一个像素上将显示它的相应字符串。
我认为使用 plotly 热图可能是做到这一点的方法。我遇到的问题是:
- 它真的很慢。如果我将我的 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 中显示。
解决方案
我不确定我是否在这里得到了正确的每个细节,但下面代码片段中的代码将在 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()
速度:
即使是这个小数字也需要一些时间来绘制,但到目前为止,我对如何加快速度没有任何建议。
推荐阅读
- javascript - 为什么我的 vue.config.js 中的chainWebpack 只在我更改后第一次工作?
- node.js - 使用 express 和 ejs 启动 Web 服务器
- r - 匹配R中两个栅格的范围?
- createjs - 我想了解 TweenJS 的“反转”属性的使用
- node.js - 为什么当我将 webpack.definePlugin 与解析的 dotenv 一起使用时 heroku 无法绑定到 $PORT
- qt - 当鼠标单击该小部件的外侧时如何隐藏 QWidget
- angular - 使用 Angular 替换数值和斜线
- elasticsearch - 包含空格、逗号和括号的 Elasticsearch 同义词
- awk - 从某行开始,每隔 n 行替换一次
- node.js - 如何查询 DynamoDB 中任何项目的 GSI 范围(即不依赖于分区键)?