改变后端
该问题似乎仅在
Tk后端存在。使用
Qt后端,窗口将保持在使用进行更新时的位置
plt.pause。
要更改后端,请在脚本开头使用这些行。
import matplotlibmatplotlib.use("Qt4agg") # or "Qt5agg" depending on you version of Qt修改中 plt.pause
如果不能更改后端,则可能有帮助。窗口不断弹出的原因是内部
plt.pause调用
plt.show()。因此,您
pause无需调用即可实现自己的功能
show。这要求首先处于交互模式
plt.ion(),然后至少调用一次
plt.show()。之后,您可以使用自定义
mypause功能更新图,如下所示。
import matplotlibmatplotlib.use("TkAgg")import matplotlib.pyplot as pltfrom time import timefrom random import randomplt.ion()# set up the figurefig = plt.figure()plt.xlabel('Time')plt.ylabel('Value')plt.show(block=False)def mypause(interval): backend = plt.rcParams['backend'] if backend in matplotlib.rcsetup.interactive_bk: figManager = matplotlib._pylab_helpers.Gcf.get_active() if figManager is not None: canvas = figManager.canvas if canvas.figure.stale: canvas.draw() canvas.start_event_loop(interval) returnt0 = time()t = []y = []while True: t.append( time()-t0 ) y.append( random() ) plt.gca().clear() plt.plot( t , y ) mypause(1)使用animation
。
最后,使用
matplotlib.animation类将使以上所有内容都过时。matplotlib页面上
matplotlib.animation.FuncAnimation显示了一个示例。



