Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python matplotlib中带有子地块的动画绘图_Python_Matplotlib_Plot - Fatal编程技术网

Python matplotlib中带有子地块的动画绘图

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(

我一直面临一些问题,试图用几个不同的子情节来制作一个情节的动画。这里有一个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(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()