3

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?

Community
  • 1
  • 1
Basj
  • 41,386
  • 99
  • 383
  • 673

1 Answers1

-1

I had the same problem with my plot. What I did was, used try..except to avoid that error being shown in the console. It's not the solution but a way to end the program gracefully.

    import Tkinter as tk

    while plt.get_fignums():
        try:
            plt.pause(0.2);
        except tk.TclError:
            break;
Vibhor Mishra
  • 31
  • 1
  • 6