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?

greatscott
  • 163
  • 2
  • 8
  • I see what you're trying to do, and I think you might try looking at using the `animation` library instead http://matplotlib.org/examples/animation/basic_example.html – Chris Zeh Nov 15 '12 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. – greatscott Nov 28 '12 at 17:28

1 Answers1

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)
Community
  • 1
  • 1
greatscott
  • 163
  • 2
  • 8
  • 1
    Neato, I wasn't familiar with this. (Reference for the pause method: http://matplotlib.org/api/pyplot_api.html?highlight=pause#matplotlib.pyplot.pause) – Chris Zeh Nov 28 '12 at 22:43
  • Thanks for the documentation link, I should have put it in the answer. – greatscott Nov 28 '12 at 23:33