2017-06-16 3 views
0

Salut J'essaie de créer un film de 15 graphes gaussiens qui se déplacent vers la gauche (c'est essentiellement ce que le code est censé faire) Cependant, mon idée pour comment créer la boucle for pour créer les 15 graphiques n'a pas créé plus de 1, cela ne fait qu'accélérer l'animation. Un code similaire a fonctionné sur matlab. Il a créé 15 courbes gaussiennes différentes. Voici un exemple de mon code. toute aide serait appréciée. MerciPython: Représentation graphique et animation de plusieurs itérations du même graphe avec Python

import numpy as np 
import matplotlib.pyplot as plt 
plt.switch_backend('agg') 
import matplotlib.animation as animation 

Gamma=0.0005 
q=1.6e-19 
m=0.067*9e-31 
B=10 
Ec=(1.0567e-34)*B/m 
#e=2.78 

#E0=0+(1.0567e-34)*x*i/m 

fig, ax = plt.subplots() 

pass 
x = np.arange(0, 3.4e-3, 1.7e-5)  # x-array, third number is interval here, x is energy 
line, = ax.plot(x, np.e**(-(x-((1.0567e-34)*1*1/m))**2/Gamma**2)) 


def animate(i): 

    for p in xrange(1,3): 
     line.set_ydata(np.e**((-(x-((1.0567e-34)*p*i/m))**2)/Gamma**2)) # update the data 
     return line, 

#Init only required for blitting to give a clean slate. 
def init(): 
    line.set_ydata(np.ma.array(x, mask=True)) 
    return line, 

ani = animation.FuncAnimation(fig, animate, np.arange(0, 2, .01), init_func=init, 
    interval=10, blit=True) 
Writer = animation.writers['ffmpeg'] 
writer = Writer(fps=20, metadata=dict(artist='Me'), bitrate=1800) 

ani.save('QHanimati.mp4', writer=writer) 

plt.show() 
+0

Où dans le code est le numéro 15? – ImportanceOfBeingErnest

Répondre

0

Vous avez actuellement exactement une ligne dans votre code. Cette ligne est mise à jour. Si vous voulez avoir plus de lignes, vous devez créer plus de lignes.
Vous devez également mettre à jour toutes ces lignes.

(Étant donné que le rôle de p ne ressort pas de l'exemple que je l'ai pris comme un certain nombre incrémenter ici. J'ai aussi limité ce 8 courbes ne pas surcharger l'image.)

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 

Gamma=0.0005 
q=1.6e-19 
m=0.067*9e-31 
B=10 
Ec=(1.0567e-34)*B/m 

fig, ax = plt.subplots() 

n = 8 # number of lines 
x = np.arange(0, 3.4e-3, 1.7e-5)   
lines = [ax.plot(x, np.e**(-(x-((1.0567e-34)*1*1/m))**2/Gamma**2))[0] for _ in range(n)] 


def animate(i): 
    for ln, line in enumerate(lines): 
     p = (ln+1)/10. 
     line.set_ydata(np.e**((-(x-((1.0567e-34)*p*i/m))**2)/Gamma**2)) # update the data 
    return lines 

#Init only required for blitting to give a clean slate. 
def init(): 
    for line in lines: 
     line.set_ydata(np.ma.array(x, mask=True)) 
    return lines 

ani = animation.FuncAnimation(fig, animate, np.arange(0, 2, .01), init_func=init, 
    interval=10, blit=True) 

plt.show() 

enter image description here

+0

Merci cela a parfaitement fonctionné. –