Fast plotting data in python(在 python 中快速绘制数据)
本文介绍了在 python 中快速绘制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 arduino 绘制来自 mpu6050 imu 的数据.MPU6050 发送数据的速度比绘图快.Arduino 代码从串口提供 6 个数据,分别是 yaw、pitch、roll、ax、ay 和 az.我需要快速剧情的建议.
I'm trying to plot data from mpu6050 imu with arduino. MPU6050 sends data faster than plot. Arduino code gives 6 data which are yaw, pitch, roll, ax,ay and az from serial port. I need suggestions for fast plot .
Python 代码:
import serial
import matplotlib.pyplot as plt #import matplotlib library
from drawnow import *
ser = serial.Serial('COM9', 115200)
yaw = 0.0
pitch =0.0
roll =0.0
ax =0.0
ay =0.0
az =0.0
o_yaw= [0]
o_pitch= [0]
o_roll= [0]
o_ax= [0]
o_ay= [0]
o_az= [0]
plt.ion()
cnt=0
def makeFig():
plt.ylim(-1000,1000)
plt.grid(True)
plt.ylabel('Magnitude')
plt.plot(olculen_ax, 'ro-', label='ax')
plt.plot(olculen_ay, 'bo-', label='ay')
plt.plot(olculen_az, 'go-', label='az')
plt.legend()
while True:
incoming=ser.readline()
if ("hand" in incoming):
incoming=incoming.split(":")
if len(incoming)==8:
yaw = float(incoming[1])
pitch = float(incoming[2])
roll = float(incoming[3])
ax = float(incoming[4])
ay = float(incoming[5])
az = float(incoming[6])
print "Split works"
else:
print incoming
o_ax.append(ax)
o_ay.append(ay)
o_az.append(az)
o_yaw.append(yaw)
o_pitch.append(pitch)
o_roll.append(roll)
drawnow(makeFig)
plt.pause(.00001)
cnt=cnt+1
if(cnt>50):
o_ax.pop(0)
o_ay.pop(0)
o_az.pop(0)
Arduino 代码(我只是添加循环.代码源自 这个):
Arduino Code (I just add loop. code derived from this):
void loop() {
if (!dmpReady) return;
while (!mpuInterrupt && fifoCount < packetSize) {
}
mpuInterrupt = false;
mpuIntStatus = mpu.getIntStatus();
fifoCount = mpu.getFIFOCount();
if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
mpu.resetFIFO();
//Serial.println(F("FIFO overflow!"));
} else if (mpuIntStatus & 0x02) {
while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
mpu.getFIFOBytes(fifoBuffer, packetSize);
fifoCount -= packetSize;
mpu.dmpGetQuaternion(&q, fifoBuffer);
mpu.dmpGetAccel(&aa, fifoBuffer);
mpu.dmpGetGravity(&gravity, &q);
mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity);
mpu.dmpGetLinearAccelInWorld(&aaWorld, &aaReal, &q);
mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
Serial.print("hand:");
Serial.print(ypr[0] * 180/M_PI);
Serial.print(":");
Serial.print(ypr[1] * 180/M_PI);
Serial.print(":");
Serial.print(ypr[2] * 180/M_PI);
Serial.print(":");
Serial.print(aaWorld.x);
Serial.print(":");
Serial.print(aaWorld.y);
Serial.print(":");
Serial.print(aaWorld.z);
Serial.println(":");
}
}
推荐答案
pyqtgraph 模块是一个很好的解决方案.它非常快速和简单.
The pyqtgraph module is a great solution. It is very fast and easy.
这是新代码:
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
from pyqtgraph.ptime import time
import serial
app = QtGui.QApplication([])
p = pg.plot()
p.setWindowTitle('live plot from serial')
curve = p.plot()
data = [0]
raw=serial.Serial('COM9', 115200)
def update():
global curve, data
line = raw.readline()
if ("hand" in line):
line=line.split(":")
if len(line)==8:
data.append(float(line[4]))
xdata = np.array(data, dtype='float64')
curve.setData(xdata)
app.processEvents()
timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.start(0)
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
这篇关于在 python 中快速绘制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:在 python 中快速绘制数据
基础教程推荐
猜你喜欢
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 筛选NumPy数组 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01