Python matplotlib中带有子地块的动画绘图
我一直面临一些问题,试图用几个不同的子情节来制作一个情节的动画。这里有一个MWE:Python matplotlib中带有子地块的动画绘图,python,matplotlib,plot,Python,Matplotlib,Plot,我一直面临一些问题,试图用几个不同的子情节来制作一个情节的动画。这里有一个MWE: import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation data=np.random.rand(4, 50, 150) Z=np.arange(0,120,.8) fig, axes = plt.subplots(2,2) it=0 for nd, ax in enumerate(
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
data=np.random.rand(4, 50, 150)
Z=np.arange(0,120,.8)
fig, axes = plt.subplots(2,2)
it=0
for nd, ax in enumerate(axes.flatten()):
ax.plot(data[nd,it], Z)
def run(it):
print(it)
for nd, ax in enumerate(axes.flatten()):
ax.plot(data[nd, it], Z)
return axes.flatten()
ani=animation.FuncAnimation(fig, run, frames=np.arange(0,data.shape[1]), interval=30, blit=True)
ani.save('mwe.mp4')
如您所见,我正在尝试使用FuncAnimation()
绘制预生成的数据。从我所看到的方法来看,这应该是可行的,但是,它输出一个大约一秒长的空白.mp4
文件,并且没有给出任何错误,所以我不知道出了什么问题
我也尝试了一些其他的方法来绘制子图(比如),但我无法使它工作,我认为我的这种方法会更简单
有什么想法吗?您可能希望更新线条,而不是为其绘制新数据。这还允许设置
blit=False
,因为保存动画时,无论如何都不会使用blit
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
data=np.random.rand(4, 50, 150)
Z=np.arange(0,120,.8)
fig, axes = plt.subplots(2,2)
lines=[]
for nd, ax in enumerate(axes.flatten()):
l, = ax.plot(data[nd,0], Z)
lines.append(l)
def run(it):
print(it)
for nd, line in enumerate(lines):
line.set_data(data[nd, it], Z)
return lines
ani=animation.FuncAnimation(fig, run, frames=np.arange(0,data.shape[1]),
interval=30, blit=True)
ani.save('mwe.mp4')
plt.show()