9

我的代码正在从 arduino 获取串行数据,对其进行处理,然后进行绘图。我使用 matplotlib 作为图形界面。每次它“吸引”时,它都会迫使人们注意它,用户将无法看到除此之外的任何东西。阻止这种情况的最佳方法是什么?(代码工作正常,除了窃取焦点)。在另一篇文章中阅读该方法后,我尝试使用 matplotlib.use('Agg') 方法,但它不起作用。(使用 MAC OS X)。

下面显示的代码是更新数据的超级简单图表,我遇到了同样的问题。我没有显示我的代码,因为如果没有正确的输入,它就无法复制粘贴这是我的代码:

import matplotlib
from matplotlib import *
from pylab import *
# import math


x=[]
y=[]
def function(iteration):
    xValue=iteration#Assigns current x value
    yValue=(1./iteration)*34#Assigns current y value

    x.extend([xValue]) #adds the current x value to the x list
    y.extend([yValue]) #adds the current y value to the y list


    clf() #clears the plot

    plot(x,y,color='green') #tells the plot what to do 
    draw() #forces a draw

def main():

    for i in range(1,25): #run my function 25 times (24 I think actually)
        function(i)
        pause(.1)

main()
4

2 回答 2

1

您是否尝试过使用 matplotlib 的交互模式?

您可以使用 ion() 将其打开(请参阅文档

如果您使用交互模式,则不需要调用 draw() 但您可能需要使用 clf() 清除图形,具体取决于您所需的输出

于 2013-10-28T10:10:10.640 回答
0

我发现使用Tkagg后端有效

import matplotlib

matplotlib.use('Tkagg')

归功于457290092

于 2020-05-06T20:23:07.850 回答