现在绘制和Matplotlib

编程入门 行业动态 更新时间:2024-10-19 03:34:55
本文介绍了现在绘制和Matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前正在开展一个项目,该项目涉及获取模拟读数,并将它们实时映射到图表上.所以为了完成这个,我通过一个 Arduino 模拟端口运行一个光电阻器,并通过 python 3.4.3 读取该数据.在python方面,我有maplotlib,并安装了drawow.如下所示的代码将绘制电阻将读取的第一个数据标记,但不会实时更新.但是,如果更改电阻,然后重新启动程序,它将连续绘制新值.我想要做的是在更改光电电阻器的值时更改图形上的值.

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. On the python side I have maplotlib, and drawnow installed. 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.

推荐答案

类似的东西

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

应该可以解决问题.

更多推荐

现在绘制和Matplotlib

本文发布于:2023-11-22 14:48:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617872.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Matplotlib

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!