0

I'm trying to produce a plot in a loop that updates itself every iteration. Working in a linux environment with python 2.6.6 I have it working, but when I run the same code in windows XP with python 2.7.3 it fails. Minimal code that has produced this error for me is:

import matplotlib.pyplot as plt

plt.ion()
plt.figure(1)

for i in range(10):
   plt.clf()
   plt.plot(i,i**2,'bo')
   plt.axis([-1,10, -1, 90])
   plt.draw()

In linux I see a blue dot that moves itself along a parabola. In MSwindows I get an empty window to begin with and then a plot with a point at (9,81) appears. This seems pretty straightforward, but maybe I'm missing something small. Any suggestions?

2
  • I see what you're trying to do, and I think you might try looking at using the animation library instead matplotlib.org/examples/animation/basic_example.html Commented Nov 15, 2012 at 23:27
  • I tried looking into the animation library, but it wasn't really what I was looking for. Maybe I misunderstood its use, but it seemed to collect all the figures during execution, but animate them only once all plots were collected. I was looking for more of a real time data display than an animation at the end of data collection. Thanks for the suggestion though. Commented Nov 28, 2012 at 17:28

1 Answer 1

1

I found the answer here: https://stackoverflow.com/a/13601492/1738884

It turns out in windows all I needed was to add a plt.pause(.01) after the plt.draw() and my plot updated every iteration as desired. So the simplified working code looks like this:

import matplotlib.pyplot as plt

plt.ion()
plt.figure(1)

for i in range(10):
    plt.clf()
    plt.plot(i,i**2,'bo')
    plt.axis([-1,10, -1, 90])
    plt.draw()
    plt.pause(.01)
Sign up to request clarification or add additional context in comments.

2 Comments

Neato, I wasn't familiar with this. (Reference for the pause method: matplotlib.org/api/…)
Thanks for the documentation link, I should have put it in the answer.

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.