python和matplotlib中的更新图 [英] python and update figure in matplotlib

查看:274
本文介绍了python和matplotlib中的更新图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经对SO进行了搜索,但是还没有找到解决我的问题的正确解决方案".我在要绘制的某些数据上运行了一个循环.在循环的每个步骤中,我都使用plt.show()绘制图形.但是,由于这是一个阻止功能,因此我一直处于困顿状态,直到手动关闭窗口,然后循环继续并显示下一个图.

I've done a search on SO but haven't quite found the right "solution" to my problem. 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(). 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.

我想做的是能够绑定一个按键事件来关闭图形并继续循环(而不是使用鼠标"X"出图形).

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).

如果这不可能,我想设置一个计时器来关闭图形并继续循环.

If this is not possible, I would like to set a timer to close the figure and continue the loop.

我所有的问题似乎都涉及到plt.show()阻止了其他所有内容的事实-可以解决这个问题吗?

All my issues seem to deal with the fact that plt.show() is blocking everything else -- any way around this?

关于我的绘图的一些注释:它们使用相同的轴,但包含一个散点图,填充框和注释-它们总是在变化.

Some notes on my plots: They use the same axes, but contain a scatter plot, fill boxes, and annotations -- which are always changing.

谢谢!

推荐答案

尝试使用matplotlib.pyplot中的ion:

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

有关ion和交互式与非交互式用法的更多信息,请此处

More info about ion and interactive vs non-interactive usage here

或者,如果您想使用按钮按下方法,请分配一个回调

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()

由于show命令正在阻塞,因此事件计时器更加棘手,因此可能必须涉及线程处理.

An event timer is more tricky because the show command is blocking, so it would probably have to involve threading.

这篇关于python和matplotlib中的更新图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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