20

I'm trying to write a python program that displays a figure for indefinite time and closes it after any keyboard key is pressed.

In fact, the python program should do the same as this Matlab code:

t = 0:0.01:2;
s = sin(2 * pi * t);

plot(t,s)

pause
close

In python I'm able to plot the figure but nothing happens after the keyboard input.

import numpy as np
import matplotlib.pyplot as plt

t = np.arange(0.0, 2.0, 0.01)
s = np.sin(2*np.pi*t)

#plt.ion()
fig = plt.figure()
plt.plot(t,s)
#plt.show()
plt.draw()

raw_input("PRESS ANY KEY TO CONTINUE.")
plt.close(fig)

So far I observed that plt.close(fig) does not do anything in conjunction with plt.show(). However, when I use plt.draw() instead, plt.close(fig) is closing the figure. Yet, when I add raw_input("PRESS ANY KEY TO CONTINUE.") into my program, the figure does not appear at all.

What am I doing wrong?

I also tried experimenting with plt.ion(), but without success.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Boris L.
  • 936
  • 2
  • 12
  • 28

3 Answers3

22

I think that using plt.waitforbuttonpress(0) could solve the trick of using raw_input():

import numpy as np
import matplotlib.pyplot as plt

t = np.arange(0.0, 2.0, 0.01)
s = np.sin(2*np.pi*t)

fig = plt.figure()
plt.plot(t,s)
plt.draw()
plt.waitforbuttonpress(0) # this will wait for indefinite time
plt.close(fig)
Guillem Cucurull
  • 1,681
  • 1
  • 22
  • 30
  • 2
    this works if we have some non-zero number for time: `plt.waitforbuttonpress(1)`. with zero, it just keeps on waiting.. and doesn't work.. what works for me is this: `plt.waitforbuttonpress(1); input(); plt.close(fig)` – MycrofD Oct 17 '19 at 11:26
11

something like this maybe ?

import numpy as np
import matplotlib.pyplot as plt

t = np.arange(0.0, 2.0, 0.01)
s = np.sin(2*np.pi*t)


fig = plt.figure()
plt.plot(t,s)
#plt.show()
plt.draw()
plt.pause(1) # <-------
raw_input("<Hit Enter To Close>")
plt.close(fig)
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • 1
    @BorisL. then please consider marking Joran's solution as "the answer". – Oliver W. Apr 08 '14 at 18:38
  • @JoranBeasley, the plot window pops up at background. I need to use my mouse to click and view the plot. Can't the plot come to the front and just by pressing `Enter` or `q` I can close it (like the way we do for gnuplot)? – hbaromega Aug 08 '15 at 00:06
1

As of writing (December 2021, using Matplotlib 3.5 on Windows), pressing the q key now seems to close a figure by default – at least with the Tk and Qt GUI backends that I tried. If another specific key is desired instead, e.g. the escape key, one might want to follow the solution provided in this discussion, which uses event handling:

import matplotlib.pyplot as plt
import numpy as np

def close_figure(event):
    if event.key == 'escape':
        plt.close(event.canvas.figure)

t = np.arange(0.0, 2.0, 0.01)
s = np.sin(2 * np.pi * t)

plt.plot(t, s)
plt.gcf().canvas.mpl_connect('key_press_event', close_figure)
plt.show()
simon
  • 1,503
  • 8
  • 16