[英]How to create a heatmap animation with plotly express?
我有一个方阵列表 M[t],其中 t 的范围从 0 到 N,我希望使用 plotly.express 创建一个动画热图。 每行/列中的条目对应一个列表,a=['a1','a2',...'aN']
animation 上的 plotly 文档相当稀疏,仅关注散点图和条形图
https://plotly.com/python/animations/
与我类似的问题已发布在
但是,用户正在使用 Jupyter 笔记本。 我只是在Mac(OS 10.15.4)上使用带有IDLE的Python 3.7
我知道如何使用ZF1C25ED1C25ED1BBF9DEE9BE5092BZ使用ZF0211321137A5A5FFF03E3EEB4664640Z或ZD50F42A37901F21D0A21D69D63C5BNEND BUCTING 34C5B7B7B7B7B7B7B7F03C54SPAR。 这是我使用的一种方法,但我确信使用 matplotlib.animation 有更有效的方法:
import numpy as np
import matplotlib.pyplot as plt
#50 matrices, each of size 4-by-4.
N = 50
M = np.random.random((50, 4,4))
#Desired labels for heatmap--not sure where to put.
labels=['a','b','c','d']
fig, ax = plt.subplots()
for t in range(50):
ax.cla()
ax.imshow(M[t])
ax.set_title("frame {}".format(t))
plt.pause(0.1)
这对你有用吗?
import numpy as np
import plotly.graph_objs as go
N = 50
M = np.random.random((N, 10, 10))
fig = go.Figure(
data=[go.Heatmap(z=M[0])],
layout=go.Layout(
title="Frame 0",
updatemenus=[dict(
type="buttons",
buttons=[dict(label="Play",
method="animate",
args=[None])])]
),
frames=[go.Frame(data=[go.Heatmap(z=M[i])],
layout=go.Layout(title_text=f"Frame {i}"))
for i in range(1, N)]
)
fig.show()
更新如果您需要添加Pause
按钮
fig = go.Figure(
data=[go.Heatmap(z=M[0])],
layout=go.Layout(
title="Frame 0",
title_x=0.5,
updatemenus=[dict(
type="buttons",
buttons=[dict(label="Play",
method="animate",
args=[None]),
dict(label="Pause",
method="animate",
args=[None,
{"frame": {"duration": 0, "redraw": False},
"mode": "immediate",
"transition": {"duration": 0}}],
)])]
),
frames=[go.Frame(data=[go.Heatmap(z=M[i])],
layout=go.Layout(title_text=f"Frame {i}"))
for i in range(1, N)]
)
fig.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.