2015-06-22 90 views
1

我目前正在研究一個涉及模擬讀數的項目,並將它們實時映射到圖上。爲了完成這個,我通過一個Arduino模擬端口運行一個光電阻,並通過python 3.4.3讀取這些數據。在python一側,我有maplotlib,並已安裝。如下所示的代碼將繪製電阻將讀取的第一個數據標記,但不會實時更新。但是,如果我改變阻力,然後重新啓動程序,它會不斷繪製新的值。我想要做的是在更改光敏電阻的值時更改圖形上的值。現在畫和Matplotlib

import serial # import from pySerial 
import numpy # import library from Numerical python 
import matplotlib.pyplot as plt # import Library from matplotlib 
from drawnow import drawnow # import lib from drawnow 

ConF = [] # create an empty array for graphing 
ArduinoData = serial.Serial('com3',9600) # set up serial connection with arduino 
plt.ion() # tell matplotlib you want interactive mode to plot data 
cnt = 0 

def makeFig(): # creat a function to make plot 
    plt.plot(ConF, 'go-') 

while True: # loop that lasts forever 
    while (ArduinoData.inWaiting()==0): # wait till there is data to plot 
     pass # do nothing 

    arduinoString = ArduinoData.readline() 
    dataArray = arduinoString 
    Con = float(arduinoString) # turn string into numbers 
    ConF.append(Con) # addinf to the array. 

    drawnow(makeFig) # call draw now to update 
    plt.pause(.000001) 
    cnt=cnt+1 
    if(cnt>50): 
     ConF.pop(0) 

我不知道在我的錯誤是,沒有任何錯誤信息......它只是繪製相同的數據點了個遍。任何幫助將是最受歡迎的。

回答

2

喜歡的東西:

fig, ax = plt.subplots() 
ln, = ax.plot([], [], 'go-') 
while True: 
    x, y = get_new_data() 
    X, Y = ln.get_xdata(), ln.get_ydata() 
    ln.set_data(np.r_[X, x], np.r_[Y, y]) 
    fig.canvas.draw() 
    fig.canvas.flush_events() 

應該做的伎倆。