4

I'm have a looping script which plots (matplotlib) and then, after plotting, waits for raw_input before continuing. This would allow me to delete any figures I don't want/need before the loop continues.

I have tried a few different ways but they never seem to properly display the figures before continuing.

# test1
x = [1, 2, 3]
plt.ion() # turn on interactive mode
for loop in range(0,3):
    y = np.dot(x, loop)
    plt.figure()
    plt.plot(x,y)
    plt.show()
    _ = raw_input("Press [enter] to continue.")

^ This one plots the figure windows but shows nothing until the loop has ended

#test2
x = [1, 2, 3]
plt.ion() # turn on interactive mode
for loop in range(0,3):
    y = np.dot(x, loop)
    plt.figure()
    plt.plot(x,y)
    plt.pause(0.02)
    plt.show()
    _ = raw_input("Press [enter] to continue.")

^ This one plots the figures fine but then freezes and the figure window crashes when the raw_input opens. The windows return to normal (are usable and zoomable) again once the full loop has finished

I've looked into the plt.show(block=False) command but haven't had any progress

Any ideas?

N.b. This question is very similar to the question here but, as you can see from the examples above, I haven't managed to get it working using those answers (which suggested plt.ion() and plt.pause(0.2)

Community
  • 1
  • 1
mjp
  • 1,618
  • 2
  • 22
  • 37
  • Have you tried plt.draw() instead? – hashcode55 Jun 20 '16 at 18:52
  • Heya @hashcode55, just tried that (replacing `plt.show()` with `plt.draw()`) and it produces the same result as test2 – mjp Jun 20 '16 at 21:08
  • I know how to make this work. I had the same issue, and solved it with a matplotlib TimedAnimation. I could get the graph updating fluently when new data arrives. I don't have time right now, but I can explain the details tomorrow. – K.Mulier Jun 20 '16 at 21:40
  • @mjp its kinda weird as the code without even replacing `plt.show()` is working perfectly on my machine. – hashcode55 Jun 20 '16 at 21:52
  • @hashcode55 as in you can see the figures and interect with them without them crashing, before you press enter to continue the loop? I have cleared everything and restarted the terminal a number of times. Not sure what could be the issue. – mjp Jun 20 '16 at 22:32
  • Yup working totally fine, which `matplotlib` version are you using? – hashcode55 Jun 20 '16 at 22:38
  • Backend is TkAgg and the version is 1.5.1 – mjp Jun 20 '16 at 22:52

2 Answers2

0

As you almost discovered, plt.pause(interval) does actually work, because, according to the documentation,

If there is an active figure, it will be updated and displayed before the pause, and the GUI event loop (if any) will run during the pause.

So if you had increased your pause interval to the following:

interval = 10 # units of seconds
plt.pause(interval)
plt.show()
input("Press Enter to continue...")
  • You would be able to interact with the plot during that interval.
  • Once the pause ends and waits for an input, you can no longer interact with the plot.
  • The plot probably crashed because you interacted with the plot after the pause while the program was waiting for raw_input(), which by the way has been changed to input() in Python 3.x and may also help prevent the figure window from crashing.
0

Another thing that you can try is something like this... Here you do incude pauses, but you can interrupt them as you wish :)

import matplotlib.pyplot as plt
# This gives a total of 30 seconds pause.. which can be interrupted
n_pauses = 3
pause_interval = 10

plt.plot(your_array)
for _ in range(n_pauses):
    try:
        plt.pause(pause_interval)
    except KeyboardInterrupt:
        break

x = input('Enter your input...')
zwep
  • 1,207
  • 12
  • 26