Matplotlib非常慢 - 无法跟上传入的Arduino数据

时间:2015-02-18 10:11:09

标签: python performance matplotlib

我有一个Arduino连接到我的笔记本电脑,并尝试使用Python和Martplotlib库绘制一些来自它的数字。 Arduino发送两个数字(arrays1& arrays2),我希望它们在Python代码(y=np.array)上配置的背景图上方绘制(因为它们来)。下面的代码可以工作,但我在Matplotlib绘图的速度方面遇到了一些问题。

当我从Arduino发送带有delay(1500)的数据时,一切正常,它们会在它们到来时重新绘制。 当我每秒从Arduino发送数据时,它开始绘制前30个值的确定,但是其余18个值的绘图被延迟,每1.5秒发生一次。 如果我没有最大化绘图窗口,或删除axvspan命令,它似乎能够每1秒绘制一次值! 如果我使用delay(100)从Arduino发送值,则Matplotlib无法比每0.5-1秒更频繁地绘制它们。

Matplotlib这么慢是否正常? 我是Python和Matplotlib的新手,所以我不知道我是否犯了任何可能延误绘图的错误。

Python代码:

import matplotlib.pyplot as plt
import serial
import matplotlib.animation as animation
import numpy as np
from scipy.interpolate import interp1d

second=0
x=np.arange(0,48)
y=np.array([33, 29, 27, 26, 25, 24, 23, 22, 21, 22, 24, 27, 31, 35, 39, 43,
47, 45, 42, 41, 39, 38, 39, 38, 40, 43, 46, 48, 46, 44, 43, 44, 45, 46, 47,
48, 49, 52, 56, 60, 62, 61, 58, 53, 49, 45, 40, 33])
f=interp1d(x,y,kind='cubic')
xint=np.arange(0,47,0.01)
yint = f(xint)

arduinoData = serial.Serial('com4',9600) #Opening the connection to the serial port

plt.ion() #Activating matplotlib's interactive mode
mng = plt.get_current_fig_manager()
mng.window.state('zoomed')

while True:
    while (arduinoData.inWaiting()==0): #Wait here until there is data
        pass #do nothing
    arduinoString = arduinoData.readline() #read the line of text from the serial port
    dataArray = arduinoString.split(',')   #Split it into an array called dataArray
    num1 = int( dataArray[0])            #Convert first element to int num1
    num2 = int( dataArray[1])            #Convert second element to int num2
    if num1 == 0 & num2 == 0:   #when it gets 0 and 0 from Arduino, clear the existing graph, plot the background plot and the axis
        plt.clf()
        plt.plot(x,y,'o',c='b')
        plt.plot(xint,yint,'-r')
        plt.title('My plot')
        plt.xlabel('Axis X')
        plt.ylabel('Axis Y)')
        plt.xlim(-0.5, 50)
        plt.ylim(0, 90)
        plt.grid()
        second=0
    else:                            # start getting the numbers that should be plotted above the background and draw the plot
        plt.plot(second, num1, 'm+') 
        plt.plot(second, num2, 'kx')
        plt.axvspan(0, second, alpha=0.5, color='cyan')
        second += 1
        fig = plt.draw()
        plt.pause(0.04)

Arduino代码:

int i=0;
int reset=0;
int array1[48]={38, 30, 28, 27, 26, 25, 25, 24, 23, 24, 27, 29, 33, 37, 40, 46, 49, 47, 44, 43, 41, 41, 42, 40, 43, 46, 49, 52, 49, 48, 47, 48, 47, 49, 50, 51, 52, 55, 59, 61, 65, 64, 61, 57, 50, 48, 42, 38}; 

int array2[48]={29, 26, 24, 23, 22, 21, 20, 19, 18, 19, 22, 24, 28, 32, 36, 40, 44, 42, 39, 37, 36, 35, 37, 36, 37, 40, 42, 44, 43, 40, 39, 40, 42, 42, 43, 45, 46, 48, 53, 58, 59, 58, 54, 50, 48, 42, 38, 31};


void setup(){
Serial.begin(9600); //turn on serial connection
}

void loop(){
  reset = 0;
  Serial.print(reset);
  Serial.print(" , ");
  Serial.println(reset);
  delay(2000);
  for (i=0;i<48;i++)
  {
    Serial.print(array1[i]);
    Serial.print(" , ");
    Serial.println(array2[i]);
    delay(1000); //Pause between data sending
    }
}

0 个答案:

没有答案