0

I wanted to do something like below:

from matplotlib import pyplot as plt
from time import sleep

plt.ion()
plt.show()
for i in range(10):
  plt.plot([1,2,3], [1,2,3]) # dummy data
  sleep(2)
  plt.cla()
  sleep(2)

But this does not work. The window appears, but nothing is drawn in it, and all buttons are dead too.

So I experimented a bit. If I paste the following into a Python shell, it shows the graph as expected.

from matplotlib import pyplot as plt
from time import sleep

plt.ion()
plt.show()
plt.plot([1,2,3], [1,2,3])
#sleep(60)

But if I comment out the last line, the window appears, but empty. I don't see why it behaves like that, can someone explain?

Ffisegydd
  • 51,807
  • 15
  • 147
  • 125
Mads Skjern
  • 5,648
  • 6
  • 36
  • 40
  • what are you expecting your first piece of code to do? – Srivatsan Oct 13 '14 at 20:07
  • I've had some issues with different interactive shells and backends behaving differently. I've had the best luck with iPython. Are you using an iPython shell? – farenorth Oct 13 '14 at 20:11
  • @ThePredator: I expect the code to show an empty window for two seconds, then a window with a line for two seconds, then over again. In my original project I want it to clear the window, then update with new data, every x seconds. But I used a simpler piece of code, which illustrates behavior that I can't explain. – Mads Skjern Oct 13 '14 at 20:49
  • @farenorth: The examples shown behaves the same with iPython and the ordinary Python shell. One important difference with iPython is, that it has interactive mode on by default, which makes the ion() superfluous. – Mads Skjern Oct 13 '14 at 20:52
  • Because you only have one thread which is dealing with both your code and the gui main loop. If the thread is sleeping, nothing happens. Use `plt.pause()` – tacaswell Oct 13 '14 at 20:52
  • and when you use ipython the gui main loop is hooked into the repl main loop so it works – tacaswell Oct 13 '14 at 20:53
  • @tcaswell: Hmmm... I can sort of understand what you are telling me, but this is far from simple. If there is only one thread, then how come the drawing is happening when... the REPL is waiting for the next command, besides when calling pause. – Mads Skjern Oct 13 '14 at 21:10
  • Look at the ipython repl/main loop integration code. And no, it is not simple. – tacaswell Oct 13 '14 at 21:18
  • Do you mean looking at the source code? That's beyond my skills. – Mads Skjern Oct 14 '14 at 02:00

0 Answers0