Unable to keep multiples animations in a figure

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Unable to keep multiples animations in a figure

Vincent FRATICELLI

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
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Unable to keep multiples animations in a figure

Benjamin Root
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" <[hidden email]> 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
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Unable to keep multiples animations in a figure

Vincent FRATICELLI

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" <[hidden email]>
> A : "Vincent FRATICELLI" <[hidden email]>
> Copie à : "Matplotlib-users" <[hidden email]>
> 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

>

>
On Dec 24, 2016 3:12 AM, "Vincent FRATICELLI" <[hidden email]> 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
> [hidden email]
> https://mail.python.org/mailman/listinfo/matplotlib-users
>
>

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Loading...