Unable to keep multiples animations in a figure

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>

Vincent,

I am away from my computer for the week, so my help will be limited. In my
book, Interactive Applications using Matplotlib, I devote chapter 3 to the
animation module. While I don't outline how one can do what you are asking
for, I do detail how animations are triggered and controlled
under-the-hood. Perhaps it can provide some inspirations?

Ben Root

···

On Dec 24, 2016 3:12 AM, "Vincent FRATICELLI" <v.fraticelli at wanadoo.fr> wrote:

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()

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
Matplotlib-users Info Page

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20161224/1b932b23/attachment.html&gt;

Hi Ben,
Thanks for the help. I keep coding as with the Fortran of my youth and some good modern books would be useful !?
Merry christmas, Vincent.
?
?
?
?

Message du 24/12/16 16:27
De : "Benjamin Root"
A : "Vincent FRATICELLI"
Copie ? : "Matplotlib-users"
Objet : Re: [Matplotlib-users] Unable to keep multiples animations in a figure

Vincent,

I am away from my computer for the week, so my help will be limited. In my book, Interactive Applications using Matplotlib, I devote chapter 3 to the animation module. While I don't outline how one can do what you are asking for, I do detail how animations are triggered and controlled under-the-hood. Perhaps it can provide some inspirations?

Ben Root

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()

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
Matplotlib-users Info Page

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20161224/0a84afc2/attachment-0001.html&gt;

···

On Dec 24, 2016 3:12 AM, "Vincent FRATICELLI" wrote: