现在绘图和Matplotlib

3
我目前正在进行一个涉及模拟读数并实时在图表上映射的项目。为此,我通过Arduino模拟端口运行光敏电阻,并通过Python 3.4.3读取数据。在Python端,我已安装了maplotlib和drawnow。如下所示的代码将绘制电阻器读取的第一个数据标记,但不会实时更新它。但是,如果我更改电阻值并重新启动程序,则会连续绘制新值。我希望随着光敏电阻值的变化,图表上的值也会相应变化。
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)

我不确定我的错误在哪里,没有错误信息...它只是一次又一次地绘制相同的数据点。任何帮助都将不胜感激。

1个回答

4

Something like:

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()

这应该能解决问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接