我正在尝试将两个animate
作为各自独立的功能subplots
。我遇到的一个动画是轮廓,而另一个是线条图。
我不确定是否可以分别在每个函数上调用animation
。我了解下面的代码称为contour
animation
,而不是line plot
。但是,如何添加line plot
使其也返回animation
?
import matplotlib.pyplot as plt
import matplotlib.animation
import numpy as np
x= np.linspace(0,3*np.pi)
X,Y = np.meshgrid(x,x)
f = lambda x,y, alpha, beta :(np.sin(X+alpha)+np.sin(Y*(1+np.sin(beta)*.4)+alpha))**2
alpha=np.linspace(0, 2*np.pi, num=34)
levels= 10
cmap=plt.cm.jet
fig1 = plt.figure()
ax1 = fig1.add_subplot(211)
ax2 = fig1.add_subplot(212)
p = [ax1.contour(X,Y,f(X,Y,0,0), levels, cmap=cmap ) ]
def update(i):
for tp in p[0].collections:
tp.remove()
p[0] = ax1.contour(X,Y,f(X,Y,alpha[i],alpha[i]), levels, cmap= cmap)
return p[0].collections
x = np.linspace(0, 10, 100)
y = np.sin(x)
line, = ax2.plot(x, y, color='k')
for n in range(len(x)):
line.set_data(x[:n], y[:n])
ax2.axis([0, 10, 0, 1])
ani = matplotlib.animation.FuncAnimation(fig1, update, frames=len(alpha),
interval=10, blit=False, repeat=True)
plt.show()