Matplotlib:在按下按键时在轴上绘制新内容 [英] Matplotlib: Plot something new in an axis upon key press

查看:77
本文介绍了Matplotlib:在按下按键时在轴上绘制新内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次用户按下键盘上的一个键时,都应该绘制一个新图.下面的示例仅使用了一个随机绘图,实际上,我会将它与在每次按下时调用不同 plot() 函数的状态机结合起来.

Each time the user presses a key on the keyboard, a new plot should be drawn. The example below uses just a random plot, in reality, I'll combine it with a statemachine that calls a different plot() function on each press.

我有一个随机图的简短代码:

I have the short minimal code with a random plot:

fig = plt.figure(1)
ax1 = fig.add_subplot(111, aspect='equal')

def plot(e):
    ax1.cla()
    ax1.plot(np.random.rand(10))

cid = fig.canvas.mpl_connect('key_press_event', plot)

plt.show()

不幸的是,没有任何反应.缺少什么或我该如何解决?我想我必须以某种方式重新绘制 ax1 否则我完全误解了某些东西...

Unfortunately, nothing happens. What is missing or how could I fix it? I guess I have to somehow redraw the ax1 or then I completely missunderstand something...

推荐答案

在函数中添加以下行:

fig.canvas.draw_idle()

这篇关于Matplotlib:在按下按键时在轴上绘制新内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆