代码的骨架粘贴在下面。当我在计时器上更新时,图形不可调整大小,并且Ctrl + C在我运行python脚本的终端中不起作用。我相信问题是一个无限循环:
on_fig_update_event() -> redraw() ->gen_data() ->on_fig_update_event()
但我不知道如何打破这个循环,我也不确定这是一个问题。 如果您有任何想法,请查看下面的代码并告诉我们:
class MyLiveChart(object):
def __init__(self, my_params):
# Other stuff involving my_params
plt.connect('button_press_event', self.on_fig_update_event)
plt.connect('motion_notify_event', self.on_fig_update_event)
plt.connect('key_press_event', self.on_fig_update_event)
plt.connect('key_press_event', self.read_input_from_console)
plt.connect('key_release_event', self.read_input_from_console)
plt.connect('button_press_event', self.read_input_from_console)
plt.connect('motion_notify_event', self.read_input_from_console)
self.run()
def on_fig_update_event(self, _):
self.redraw()
def redraw(self):
plt.clf()
self.gen_data()
plt.draw()
def run(self):
self.figure = plt.figure(figsize=(16, 8))
self.gen_data()
# Other stuff
plt.show()
def gen_data(self):
# Other stuff
fig = plt.subplot2grid((num_cols, num_rows), (col, row), rowspan=rowspan,
colspan=colspan)
timer = fig.figure.canvas.new_timer(interval=10000, callbacks=[(self.on_fig_update_event, [None], {})])
timer.start()
# Other stuff
plt.grid()
plt.tight_layout()