使用matplotlib同时设置两个或多个图形的动画

2024-05-12 12:30:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图同时制作两个matplotlib图形的动画。我编写了一个脚本,创建了matplotlib.animation.FuncAnimation的两个实例,但其中只有一个是动画的,另一个是静态的。

下面是一个最小的工作示例:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

x=np.linspace(-np.pi,np.pi,100)

# Function for making figures
def makeFigure():
    fig=plt.figure()
    ax=fig.add_subplot(1,1,1)

    # Plot a line
    line,=ax.plot(x,np.sin(x))

    return fig,ax,line

# Frame rendering function
def renderFrame(i,line):
    # Apply a phase shift to the line
    line.set_data(x,np.sin(x-i*2*np.pi/100))
    return line,

# Make the figures
figcomps1=makeFigure()
figcomps2=makeFigure()

# Animate the figures
for figcomps in [figcomps1,figcomps2]:
    fig,ax,line=figcomps
    anim=animation.FuncAnimation(fig,renderFrame,fargs=[line])

plt.show()

FuncAnimation是否能够同时为两个图形设置动画?如果没有,是否有其他方法可以同时设置两个matplotlib图形的动画?


Tags: theimport图形matplotlibnplinepifig