1

I was given some code that is supposed to simulate an animation of a y-array being refreshed over time in a single plot figure. I want to show the plot in my terminal window (using Spyder 3.3.6/Anaconda 1.9.7 on a MAC 10.13.14) that refreshes the y-values after every interval over the 'for' loop. I have been given code from an instructor that is supposed to do the plotting - on his video it looks like an animation - using calls to various ax subroutines - but in my hands all I get is a single plot with the time=0 starting array values.

I have researched numerous blog posts for similar situations, and have studied the matplotlib manual; I have tried playing around with the various commands, and their placement, and arguments. The best I have done is to plot all the y-arrays on top of each other on a single plot (by commenting out the ax.clear() and plt.show(block=false) commands. I can easily generate new plots for each y-array. But I am in the woods as to what is going on with my code.

I am submitting the complete code so you can run it and see the results. Appreciate any light you can shed on this. Thanks.

import matplotlib.pyplot as plt
import numpy as np

y = numpy.zeros(10)
#set up plot
fig, ax=plt.subplots()
ax.plot(y)
ax.set_ylim([0,1])
plt.show(block=False)
#instructor note: "the block=False flag tells Python to throw the plot on the screen then keep moving".

for i in range(5):
    y = np.random.random([10,1])
    ax.clear()
    ax.plot(y)
    ax.set_ylim([0,1])
    plt.show(block=False)
    fig.canvas.draw()
#Instructor note: "These commands will update the plot to show the latest y values, and then keeps moving". 

ax.clear()
ax.plot(y)
ax.set_ylim([0,1])
plt.show
fig.canvas.draw()
JohnE
  • 11
  • 2
  • 3
    Replace `fig.canvas.draw()` with `plt.pause(0.2)`. In general, see the matplotlib documentation for how to produce animations - rather then some "instructor's video". – ImportanceOfBeingErnest Jan 28 '20 at 18:54
  • Made the changes; same results. – JohnE Jan 28 '20 at 20:10
  • It should work with those changes. If it doesn't, you can provide more information about where and how you run your code, versions in use etc. etc. – ImportanceOfBeingErnest Jan 28 '20 at 20:24
  • Apart from that "numpy.zeros" and the "plt.show" without parenthesis the example works if you launch as a "py" script. To make it works on conda I think you may need to look at this: https://stackoverflow.com/questions/39658717/plot-dynamically-changing-graph-using-matplotlib-in-jupyter-notebook – Fabrizio Jan 29 '20 at 12:28

0 Answers0