I'm trying to create a program that will allow users to plot graphs on multiple subplots along with a large graph that is overlaid across all subplots. Now the user has the option to animate their graphs if they want as well. The animation seems to be working well when either animating on the multiple small subplots and when animating the large graph with still figures in the smaller subplots. The problem arises, however, in the case that the user wants to put animations into both the smaller subplots and the larger overall plot (shouldn't happen often but might come up for whatever reason). Here is a sample of what I have:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def animate_graph(fig, ax, x, y):
line, = ax.plot([], [])
def init():
line, = ax.plot([], [])
return line,
def animate(i, x, y):
x = x[:int(i+1)]
y = y[:int(i+1)]
line.set_data(x, y)
line.axes.axis([0, 5, -1, 1])
return line,
ani = animation.FuncAnimation(fig, animate, init_func= init, interval = 20, fargs = [x, y], blit = True)
#create figure and subplots
fig = plt.figure()
ax1 = fig.add_axes([0.1, 0.1, 0.8, 0.4])
ax2 = fig.add_axes([0.1, 0.5, 0.8, 0.4])
#create axis in front with dimensions that span both subplots
ax_full = fig.add_axes([0.1, 0.1, 0.8, 0.8])
#make background of large axis transparent
ax_full.patch.set_alpha(0)
#create data arrays
x = np.linspace(0, 5, 100)
y1 = np.sin(2*np.pi*x)
y2 = np.cos(2*np.pi*x)
y_full = [0.5 if i < 2.5 else -0.5 for i in x]
#call animation function
animate_graph(fig, ax1, x, y1)
animate_graph(fig, ax2, x, y2)
animate_graph(fig, ax_full, x, y_full)
It works when I call animate_graph the first two time, but if I add in the third animate_graph call, the animation becomes very jumpy and not timed together. I'm assuming it's because matplotlib is running into timing issue between timing the animation and displaying the plots on top of each other but I'm not sure how I would go about solving it. Any help would be great, thanks!