1

From here I found this code:

import random
from matplotlib import pyplot as plt
import numpy as np

plt.ion() # interactive mode
ydata = [0] * 50 

# make plot
ax1 = plt.axes() 
line, = plt.plot(ydata)
plt.ylim([0, 100]) # set the y-range

while True:
    randint = int(random.random() * 100)
    ymin = float(min(ydata)) - 10
    ymax = float(max(ydata)) + 10
    plt.ylim([ymin,ymax])
    ydata.append(randint)
    del ydata[0]
    line.set_xdata(np.arange(len(ydata)))
    line.set_ydata(ydata)  # update data
    plt.draw() # update plot

I get a plot window that pops up, but no data appears and nothing gets redrawn...any idea what I'm doing wrong?

lollercoaster
  • 15,969
  • 35
  • 115
  • 173
  • On Mac 10.6.8 with python3.3 and python2.7, matplotlib 1.3.0, numpy 1.7.1 everything is fine. Do you have the latest packages? What happens if you use another backend - if I insert `import matplotlib; matplotlib.use('Qt4Agg')` then there is nothing redrawn, too. [You might need another backend](http://stackoverflow.com/questions/5091993/list-of-all-available-matplotlib-backends)?! – Stefan Bollmann Sep 30 '13 at 23:43
  • 1
    What does `matplotlib.get_backend()` return? Try adding a `plt.pause(.1)` in your loop. – tacaswell Oct 01 '13 at 00:22
  • my backend is `'WXAgg` - I don't know if that's good or bad. and the pause seems to work! – lollercoaster Oct 01 '13 at 02:55
  • possible duplicate of [make matplotlib draw() only show new point](http://stackoverflow.com/questions/16447812/make-matplotlib-draw-only-show-new-point) – tacaswell Oct 01 '13 at 15:07

1 Answers1

5

The issue you are having is due to the way that gui mainloops work. When ever you plot call draw events get added to the queue of events for the main loop to process. If you add them as fast as possible the loop can never clear it's queue and actually draw to the screen.

Adding a plt.pause(.1) will pause the loop and allow the main loop (at the risk of being anthropomorphic) 'catch it's breath' and update the widgets on the screen

Related:

Community
  • 1
  • 1
tacaswell
  • 84,579
  • 22
  • 210
  • 199