1

I am trying to run this code below but it is not working properly. I've followed the documentation from matplotlib and wonder what is wrong with this simple code below. I am tryting to animate this into jupyter notebook with anaconda distro. My python version is 2.7.10.

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

fig = plt.figure()

def init():
    m = np.zeros(4800)
    m[0] = 1.6
    return m

def animate(i):
    for a in range(1,4800):
        m[a] = 1.6
        m[a-1] = 0
    return m    

anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=200, interval=20, blit=True)

plt.show()
2
  • 2
    Could you elaborate on what is not working? What do you get and what do you expect? One problem with your code certainly is that you're not plotting anything. Have you looked at the matplotlib animation examples? Commented Nov 7, 2015 at 22:00
  • What I am trying here is to get the value 1.6 and move it to the right for every interaction. So into an array of 5 for example on the time t = 1 I would like to have [1.6,0,0,0,0], on the time t=2 [0,1.6,0,0,0] and so on. I've taken a look at the examples, but all of then look to complex for the simplicity I am looking for here. Was am I clear ? Commented Nov 7, 2015 at 22:22

1 Answer 1

4

You need to create an actual plot. Just updating a NumPy array is not enough. Here is an example that likely does what you intend. Since it is necessary to access the same objects at multiple places, a class seems better suited as it allows to access instance attributes via self:

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



class MyAni(object):
    def __init__(self, size=4800, peak=1.6):
        self.size = size
        self.peak = peak
        self.fig = plt.figure()
        self.x = np.arange(self.size)
        self.y = np.zeros(self.size)
        self.y[0] = self.peak
        self.line, = self.fig.add_subplot(111).plot(self.x, self.y)

    def animate(self, i):
        self.y[i - 1] = 0
        self.y[i] = self.peak
        self.line.set_data(self.x, self.y)
        return self.line,

    def start(self):
        self.anim = animation.FuncAnimation(self.fig, self.animate,
            frames=self.size, interval=20, blit=False)

if __name__ == '__main__':
    ani = MyAni()
    ani.start()

    plt.show()
Sign up to request clarification or add additional context in comments.

2 Comments

Why did you have to add the name = 'main' at the end ?
The part below if __name__ == '__main__': will only be executed if the module ( this source file) is used as a program, i.e. at the console: python my_program.py. This part will be skipped if this module is imported by another module (import my_program). This is not specific to this animation in any way.

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.