0

Hi I am trying to create a movie of 15 Gaussian graphs that move to the left (thats essentially what the code is suppose to do) However, my idea for how to create the for loop to create the 15 graphs has not created more than 1, it only speeds up the animation. A similar code worked on matlab. It created 15 different Gaussian curves. Here is a sample of my code. any help would be appreciated. Thanks

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()
1
  • Where in the code is the number 15? Commented Jun 17, 2017 at 11:15

1 Answer 1

1

You currently have exactly one line in your code. This line gets updated. If you want to have more lines, you need to create more lines.
You then also need to update all of those lines.

(Since the role of p isn't clear from the example I took it as some incrementing number here. I also restricted this to 8 curves not to overcrowd the 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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.