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?

4
  • 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?! Commented Sep 30, 2013 at 23:43
  • 1
    What does matplotlib.get_backend() return? Try adding a plt.pause(.1) in your loop. Commented Oct 1, 2013 at 0:22
  • my backend is 'WXAgg - I don't know if that's good or bad. and the pause seems to work! Commented Oct 1, 2013 at 2:55
  • possible duplicate of make matplotlib draw() only show new point Commented Oct 1, 2013 at 15:07

1 Answer 1

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:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.