There are a lot of questions regarding how to use matplotlib, especially for how to either update or output a new chart per loop iteration. In particular this question is in relation to matplotlib and jupyter-notebook
For example:
Use a loop to plot n charts Python
The answer is as follows:
import matplotlib.pyplot as plt
x=[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
y=[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
for i in range(len(x)):
plt.figure()
plt.plot(x[i],y[i])
which appears to work...
until we add anything else:
for example, add a sleep statement after each plot:
import matplotlib.pyplot as plt
from time import sleep
x=[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
y=[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
for i in range(len(x)):
plt.figure()
plt.plot(x[i],y[i])
sleep(2)
and suddenly it doesn't work.
One can also use any form of print to see the same issue
import matplotlib.pyplot as plt
from time import sleep
x=[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
y=[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
for i in range(len(x)):
plt.figure()
plt.plot(x[i],y[i])
print('loop: ', i)
So how can I print out a new plot for each loop in a jupyter-notebook?