0

It happens when I run this as a script from anaconda prompt

import time
import matplotlib.pyplot as plt

plt.ion()

plt.plot([1,4,2])
plt.draw()
plt.pause(0.01)
time.sleep(0.1)

h = 0
for n in range(0,9500):
    h += 1
    print(h)
    time.sleep(2)

But if I run this code line by line in command window

>>>import matplotlib.pyplot as plt
>>>plt.ion()
>>>plt.plot([1,4,2])
>>>#window is responding and I can do anything I want here

Why is this happening? I want to be able to see the plot while it's computing.

Alex Azazel
  • 332
  • 5
  • 18
  • Don't use `time.sleep` in a code which you don't want to "sleep" (become unresponsive). – ImportanceOfBeingErnest Aug 04 '17 at 08:16
  • 1
    @ImportanceOfBeingErnest I replaced `time.sleep()` with another loop but still happens, I found a somewhat satisfying solution of calling `plt.pause(0.01)` once in a while, somebody answered and then deleted the answer for some reason but it's easy to see how it could go wrong so I'm not putting that as an answer to my question. I think matplotlib library is not well made and that's the problem – Alex Azazel Aug 04 '17 at 20:16
  • This has little to do with how matplotlib is designed. It's rather a problem that you want to do two seperate things at once in a single thread. I.e. you want to perform some calculation independend of the matplotlib window, but also want to keep the matplotlib window responsive. "Keeping responsive" means that the event loop of the window needs to be able to handle events (mouse clicks etc.). The way to do that is indeed `plt.pause` (as it calls `flush_events` internally). More complex solution could use multithreading, but it all depends on what you really want to do (instead of `print h`). – ImportanceOfBeingErnest Aug 05 '17 at 08:22
  • I would expect it already relies on multithreading but apparently it's something I should account for from outside – Alex Azazel Aug 12 '17 at 14:59

0 Answers0