[Matplotlib-users] Unable to keep multiples animations in a figure

Vincent FRATICELLI v.fraticelli at wanadoo.fr
Sat Dec 24 03:05:08 EST 2016


Hi (and sorry for my poor english),
 
I am a French Physics teacher, beginning with Python.
 
I would like to plot animations of magnetic field lines. In the future, my aim would be to control each animation with the mouse button. The plot would began at the first click and stop at the next one.
For now, I simply try to plot two successives animations of field lines using funcAnimation. My problem is that the first animation is cleared when the second is plotted. How could I keep all in the figure ?
If anyone could help ?
Best regards, Vincent.
 
Codes :
Unable to find the solution, I have removed all the loops in the script and simply made a crude copy of the first part of the script to repeat the animation. But even this don't work !
****************************************************************************
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
from scipy.integrate import odeint
 
"""Ce script anime progressivement une ligne de champ à partir d'un point indiqué"""
 
fig = plt.figure()
ax = plt.axes(xlim=(-1,1), ylim=(-1, 1))
 
L=np.linspace(0,1,100)
plt.plot(L,L)
 
y0=(0.2,0.1)
line, = ax.plot([], [], lw=2)
 
def animate(i):
    def F(y,t):        
        dy=[0,0]
        dy[0]=y[1]
        dy[1]=-y[0]
        return dy    
 
    t_min=0;t_max=10;dt=0.1
    t = np.arange(t_min,i*t_max/20,dt)     
    y=odeint(F,y0,t)
    line.set_data(y[:,0],y[:,1])
    return line,
 
anim = animation.FuncAnimation(fig, animate,frames=10,repeat=0)
 
# Bloc suivant identique au premier avec y0 différent
# Son exécution efface la première ligne
 
"""y0=(0.8,0.1)
line2, = ax.plot([], [], lw=2)
def animate2(i):
    def F(y,t):        
        dy=[0,0]
        dy[0]=y[1]
        dy[1]=-y[0]
        return dy    
 
    t_min=0;t_max=10;dt=0.1
    t = np.arange(t_min,i*t_max/20,dt)     
    y=odeint(F,y0,t)
    line2.set_data(y[:,0],y[:,1])
    return line2,
 
anim2 = (animation.FuncAnimation(fig, animate2,frames=10,repeat=0))
"""
plt.show()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20161224/028174e4/attachment.html>


More information about the Matplotlib-users mailing list