python - 常规热图有效,但图工厂热图在 Plotly 中失败

标签 python python-3.x plotly heatmap

数据

fin_list

[[None, 9.1, 8.8, 8.7, 8.8, 9.1, 9.2, 9.2, 9.0, 9.6],
 [None, 8.8, 8.5, 8.8, 8.8, 8.8, 9.1, 8.9, 8.8, 9.7],
 [None, 8.8, 8.6, 8.9, 9.6, 9.0, 8.8, 8.7, 9.0, 9.9],
 [None, 9.1, 9.7, 8.9, 8.8, 8.7, 9.7, 9.1, 9.7, 9.6],
 [None, 8.5, 8.5, 8.5, 8.7, 8.6, 8.0, 9.0, 9.9, 9.5],
 [None, 8.5, 9.4, 8.7, 9.1, 9.7, 8.4, 8.6, 8.4, 9.9],
 [None, 8.6, 8.9, 9.2, 9.8, 8.8, 9.1, 9.4, None, None],
 [None, 7.6, 7.9, 7.5, 5.5, 6.0, 4.1, None, None, None]]

排序(系列['剧集'])

[1, 2, 3, 4, 5, 6, 7, 8]

y_episode

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

使用 Graph_objects 的热图代码

import plotly.graph_objects as go

fig = go.Figure(data=go.Heatmap(
                   x=y_episodes,
                   y=sorted(series['episodes']),
                   z=fin_list,
                   colorscale = 'OrRd',
                   hoverongaps = False))

输出

enter image description here

使用热图的代码 plotly.figure_factory

import plotly.figure_factory as ff

fig = ff.create_annotated_heatmap( 
                   x=sorted(series['episodes']),
                   y=y_episodes,
                   z=fin_list,
                   colorscale = 'OrRd')

Error : PlotlyError: oops, the x list that you provided does not match the width of your z matrix

但我的轴显然是正确的,因为它们在上图中起作用。

print(len(sorted(series['episodes']))) #x_axis = 8
print(len(y_episodes)) #y_axis = 11
print(len(fin_list)) #z_axis = 8

我的最终目标是在热图上进行注释,如果有在使用 graph_objects(而不是图形工厂)的代码中添加注释的解决方法,请告诉我。

最佳答案

使用 numpy.nan而不是 float 类型.您可以替换 fin_list 中的值使用嵌套列表理解:

list_nans=[[np.nan if y is None else y for y in x] for x in fin_list]

剧情:

enter image description here

代码:

import plotly.figure_factory as ff
import numpy as np

fin_list = [[None, 9.1, 8.8, 8.7, 8.8, 9.1, 9.2, 9.2, 9.0, 9.6],
             [None, 8.8, 8.5, 8.8, 8.8, 8.8, 9.1, 8.9, 8.8, 9.7],
             [None, 8.8, 8.6, 8.9, 9.6, 9.0, 8.8, 8.7, 9.0, 9.9],
             [None, 9.1, 9.7, 8.9, 8.8, 8.7, 9.7, 9.1, 9.7, 9.6],
             [None, 8.5, 8.5, 8.5, 8.7, 8.6, 8.0, 9.0, 9.9, 9.5],
             [None, 8.5, 9.4, 8.7, 9.1, 9.7, 8.4, 8.6, 8.4, 9.9],
             [None, 8.6, 8.9, 9.2, 9.8, 8.8, 9.1, 9.4, None, None],
             [None, 7.6, 7.9, 7.5, 5.5, 6.0, 4.1, None, None, None]]

y_sorted=[1, 2, 3, 4, 5, 6, 7, 8]

y_episodes=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

list_nans=[[np.nan if y is None else y for y in x] for x in fin_list]


fig = ff.create_annotated_heatmap( 
                   #x=y_episodes,
                   #y=y_sorted,
                   z=list_nans,
                   #colorscale = 'OrRd'
)
fig.show()

改进空间

请注意,我已经注释掉了 x 和 y 参数。正如您设置的那样,它无法正常工作。原因可能是两个片段的 x 和 y 参数不同:

# go.Heatmap()
x=y_episodes,
y=sorted(series['episodes'])

# ff.create_annotated_heatmap()
x=sorted(series['episodes']),
y=y_episodes,

我会留给你去弄清楚那个特定的部分。不过还好ff.create_annotated_heatmap()正如您从我的建议中看到的那样,不需要这些参数。

此外,您可以通过屏蔽或删除 nan 使图形本身看起来更好。可视化中的值。但提供的建议至少会处理 TypeError: '<' not supported between instances of 'float' and 'NoneType' ,所以我希望这是让您更进一步所需要的。

关于python - 常规热图有效,但图工厂热图在 Plotly 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61201802/

相关文章:

python - 这个双列表理解可以直接进入数组吗(有没有更 Pythonic 的方式?)

python - 如何在 Pandas 中分组和虚拟

linux - 获取 lsof 命令 Python 3.5 的输出

r - 如何为包含两个(或更多)几何对象的 ggplotly() 对象自定义悬停文本?

python - Plotly Scattermapbox : Is there a way to include some text above and below the markers?

python - wxPython 浏览器大小

python - 批处理文件重命名: zero padding time with regex?

python-3.x - MXNet:nn.Activation 与 nd.relu?

python-3.x - 如何用新的附加图片训练面部识别器?

r - 在 R 中创建绘图图表以在计数和百分比之间切换