简体   繁体   English

python和matplotlib中的更新图

[英]python and update figure in matplotlib

I've done a search on SO but haven't quite found the right "solution" to my problem. 我已经对SO进行了搜索,但还没有找到解决问题的正确“解决方案”。 I am running a loop on some data that I wish to plot. 我在一些我希望绘制的数据上运行循环。 At each step of the loop -- I plot the figure with plt.show(). 在循环的每一步 - 我用plt.show()绘制图形。 However, since this is a blocking function, i am stuck until I manually close the window and then the loop continues and the next plot shows up. 但是,因为这是一个阻塞功能,所以直到我手动关闭窗口然后循环继续并且下一个图显示为止。

What I would like to do is be able to bind a key press event to close the figure and continue the loop (rather than using the mouse to "X" out of the figure). 我想要做的是能够绑定按键事件以关闭图形并继续循环(而不是使用鼠标从图中“X”)。

If this is not possible, I would like to set a timer to close the figure and continue the loop. 如果这是不可能的,我想设置一个计时器来关闭数字并继续循环。

All my issues seem to deal with the fact that plt.show() is blocking everything else -- any way around this? 我所有的问题似乎都解决了plt.show()阻止其他一切的事实 - 任何解决方法?

Some notes on my plots: They use the same axes, but contain a scatter plot, fill boxes, and annotations -- which are always changing. 关于我的情节的一些注释:它们使用相同的轴,但包含散点图,填充框和注释 - 它们总是在变化。

Thanks! 谢谢!

Try using ion from matplotlib.pyplot : 尝试使用matplotlib.pyplot ion

import matplotlib.pyplot as pp
pp.ion()
fig = pp.figure()

More info about ion and interactive vs non-interactive usage here 有关ion和交互与非交互用法的更多信息,请点击此处

Alternatively if you want to go with the button press approach assign a callback 或者,如果您想使用按钮按下方法来指定回调

def moveon(event):
    pp.close()

cid = fig.canvas.mpl_connect('key_press_event', moveon)
pp.show()

An event timer is more tricky because the show command is blocking, so it would probably have to involve threading. 事件计时器更棘手,因为show命令是阻塞的,因此它可能必须涉及线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM