This code (coming from here) works well to do some plot, and update with new plots in a for loop:
import numpy as np
from matplotlib import pyplot as plt

plt.axis([-50,50,0,10000])
plt.ion() # interactive mode on

X = np.arange(-50, 51)
for k in range(1,5):
print k
Y = [x**k for x in X]
plt.plot(X, Y)
plt.draw()
plt.pause(1)
It works, but it has 2 (minor) drawbacks:

  • A warning message:
    C:\Python27\lib\site-packages\matplotlib\backend_bases.py:2399: MatplotlibDeprecationWarning: Using default event loop until function specific to this GUI is implemented warnings.warn(str, mplDeprecation)
  • If I close the main plot window during the loop, I get:
    File "C:\Python27\lib\lib-tk\Tkinter.py", line 964, in update self.tk.call('update')
    _tkinter.TclError: can't invoke "update" command: application has been destroyed

What's the clean way to do non-blocking plots in a for loop?


View Solution