matplotlib使用ion()动态更新图像,而不设置为活动窗口

问题:在每次更新时使交互式matplotlib窗口不会弹出到前面(Windows 10)

在使用matplotlib画动图时,遇到一个问题。在交互式图中,窗口始终位于前面,就好像这样会使matplotlib绘图窗口弹出,因为活动窗口已成为默认行为。
该如何停用它?我不希望窗口每隔5秒就出现一次。

我希望它保留在我放置它的z顺序中,无论是在活动窗口的前面还是后面。

解决办法1:修改 plt.pause

窗口不断弹出的原因来自于内部plt.pause调用plt.show()。因此,可定义pause无需调用即可实现自己的功能show。这需要首先处于交互模式plt.ion(),之后,可以使用自定义mypause功能更新绘图,如下所示。

mport matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
from time import time
from random import random

plt.ion()
# set up the figure
fig = 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)
            return


t0 = time()
t = []
y = []
while True:
    t.append( time()-t0 )
    y.append( random() )
    plt.gca().clear()
    plt.plot( t , y )
    mypause(1)

解决办法2:改变画图GUI后端

import matplotlib
matplotlib.use("Qt4agg") # or "Qt5agg" depending on you version of Qt

参考[https://xbuba.com/questions/45729092]1

[1]:在每次更新时使交互式matplotlib窗口不会弹出到前面(Windows 7) https://xbuba.com/questions/45729092

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值