数据
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))
输出
使用热图的代码 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]
剧情:
代码:
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/