In a Coursera class about plotting in Python with Matplotlib, I was taught to make animated plots by using a FuncAnimation
object. Why is this necessary? The obvious technique would be to use a for
loop that updates the plot and pauses on each iteration. That seems a lot easier.
I've tried this, and it didn't work. Here's a simple example:
import matplotlib.pyplot as plt
import numpy as np
import time
%matplotlib notebook
plt.plot([0, 1, 2], [0, 1, 0])
l = plt.gca().get_children()[0]
for i in range(5):
l.set_ydata([0, 1, i/10])
plt.show()
time.sleep(1)
The result is that only the final plot is shown, after a 5-second delay. Why doesn't this work?
EDIT: I should have specified that I'm running this code in a Jupyter notebook.