我有以下极简代码,效果很好:一个连续的 while 循环不断地绘制我的数据,如果我按下退出键,绘制就会停止。现在,如果关闭 matplotlib 窗口,则会因为该plt.pause
命令而出现一个新窗口,但现在key_event
不再附加该窗口。有没有办法保持新出现的窗口和key_event
?
代码:
import matplotlib.pyplot as plt
import numpy as np
keep_ploting = True
def action():
def key_event(event):
if event.key == 'escape':
global keep_ploting
keep_ploting = False
fig = plt.figure()
while keep_ploting:
plt.clf()
x = np.linspace(1, 10, 100)
y = np.random.weibull(2,100)
plt.plot(x, y)
plt.pause(1e-1)
fig.canvas.mpl_connect('key_press_event', key_event)
action()
当您关闭窗口时,它会创建新的figure
,您应该使用gcf()
(获取当前数字)分配event
给新的figure
while keep_ploting:
plt.clf()
x = np.linspace(1, 10, 100)
y = np.random.weibull(2,100)
plt.plot(x, y)
plt.pause(1e-1)
fig = plt.gcf() # get current figure
fig.canvas.mpl_connect('key_press_event', key_event)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句