Simple Matplotlib Animate Not Working
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 trytin
Solution 1:
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()
Post a Comment for "Simple Matplotlib Animate Not Working"