I made a plot animation. The relevant part of my code is this:
#Create Gif Animation
upd = 24
progression = 10 #x-axis
xstart = -5
xlim = [xstart,5]
ylim = [-70,80]
line, = ax.plot(xlim, ylim, 'r-', linewidth=2)
def update(ii):
line.set_xdata(xstart + (ii)/upd)
return line
anim = FuncAnimation(fig, update, frames=np.arange(0, progression * upd) + 1, interval = (1000/upd)*progression)
print('Progress... Creating Animation')
anim.save('/home/artur/Desktop/anim.gif', writer='imagemagick', fps=upd)
-
line, = ax.plot(xlim, ylim, 'r-', linewidth=2)
creates a vertical line and
def update(ii):
line.set_xdata(xstart + (ii)/upd)
return line
anim = FuncAnimation(fig, update, frames=np.arange(0, progression * upd) + 1, interval = (1000/upd)*progression)
creates the animation, handles the update rate etc. The red line represents time progression.
Now, how do I make this work for two stacked graphs? How do I draw the same line just over both ax objects?
In this case two pd.DataFrames
are plotted on two axis. I hope I could make my problem clear.
Edit
This problem was described here already but HYRY provided a much better solution.
#Create Gif Animation
upd = 1
progression = 20 #x-axis
xstart = -10
xlim, ylim = [xstart,10], [0, 40]
xlim2, ylim2 = [xstart,10], [-70, 70]
line = plt.Line2D([0, 0],
[axes[1].get_position().y0,
axes[0].get_position().y1],
color="red", ls="-",
transform=BlendedGenericTransform(axes[0].transData, fig.transFigure))
def update(ii):
xdata = xstart + (ii)/upd
line.set_xdata(xdata)
fig.add_artist(line)
return line
anim = FuncAnimation(fig, update, frames=np.arange(0, progression * upd) + 1, interval = (1000/upd)*progression)
print('Progress: Creating Animation')
anim.save('/home/artur/Desktop/anim3.gif', writer='imagemagick', fps=upd)