简体   繁体   English

现在绘制和Matplotlib

[英]Draw now and Matplotlib

I am currently working on a project that involves taking analog readings, and mapping them real time on a graph. 我目前正在从事一个涉及获取模拟读数并将其实时映射到图形上的项目。 So to complete this I am running a photo resister through an Arduino Analog port and am reading that data via python 3.4.3. 因此,要完成此操作,我要通过Arduino模拟端口运行光敏电阻,并通过python 3.4.3读取该数据。 On the python side I have maplotlib, and drawnow installed. 在python端,我有maplotlib,并安装了drawow。 The code as it is shown below will plot the first data marker that the resistor will read, but will not update it real time. 如下所示的代码将绘制电阻将读取的第一个数据标记,但不会实时更新。 However if I change the resistance and then restart the program it will then plot the new value continually. 但是,如果我改变电阻然后重新启动程序,它将连续绘制新值。 What I want it to do is change the value on the graph as I change the value of the photo resistor. 我想要做的是在更改光电电阻器的值时更改图表上的值。

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)

I am not sure where my mistake is, there is no error message... it just plots the same data point over and over. 我不确定我的错误在哪里,没有错误消息...它只是一遍又一遍地绘制相同的数据点。 Any help would be most welcome. 任何帮助将是最欢迎的。

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

should do the trick. 应该可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM