0

I am trying to animate two separate functions as their own individual subplots. The issue I'm running into one animation is a contour and the other is a line plot.

I'm not sure if I can call the animation on each function separately. I understand the code below calls the contour animation and not the line plot. But how do I add the line plot so it also return an 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()

Animation

jonboy
  • 415
  • 4
  • 14
  • 45
  • @DizietAsahi, I've had a look. Its not a complete duplicate. They have two identical plots using `line1, = ax1.plot([], [], lw=2)` but I need to include this within a `contour` function – jonboy Apr 15 '19 at 09:54
  • although not a perfect duplicate, the logic is the same. Use one `update()` function and update both objects in the body of the function, and return a list of updated artists – Diziet Asahi Apr 15 '19 at 11:24

0 Answers0