我有一个平方矩阵列表M [t],其中t的范围是0到N,我希望使用plotly.express创建一个动画的热图。每行/列中的条目都对应一个列表, a = ['a1','a2',...'aN']
关于动画的剧情文档很少,只关注散点图和条形图
https://plotly.com/python/animations/
类似于我的问题发布在
How to animate a heatmap in Plotly
但是,用户正在使用Jupyter笔记本电脑工作。我只是在Mac(OS 10.15.4)上将Python 3.7与IDLE一起使用]
我知道如何使用matplotlib或seaborn创建基本动画,但是我喜欢plotly express附带的内置开始/停止按钮。这是我使用的一种方法,但是我确信使用matplotlib.animation有更有效的方法:
import numpy as np
import matplotlib.pyplot as plt
N = 50
M = np.random.random((50, 10, 10))
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()
UPDATE如果您需要添加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()