matplotlib - How to close a python figure by keyboard input? -
i'm trying write python program displays figure indefinite time , closes after keyboard key pressed.
in fact, python program should same matlab code:
t = 0:0.01:2; s = sin(2 * pi * t); plot(t,s) pause close
in python i'm able plot figure nothing happens after keyboard input.
import numpy np import matplotlib.pyplot 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 key continue.") plt.close(fig)
so far observed plt.close(fig)
not in conjunction plt.show()
. however, when use plt.draw()
instead, plt.close(fig)
closing figure. yet, when add raw_input("press key continue.")
program, figure not appear @ all.
what doing wrong?
i tried experimenting plt.ion()
, without success.
something maybe ?
import numpy np import matplotlib.pyplot 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 close>") plt.close(fig)
Comments
Post a Comment