gpt4 book ai didi

使用 pyqtgraph 的 python 实时绘图

转载 作者:太空宇宙 更新时间:2023-11-03 12:44:18 24 4
gpt4 key购买 nike

我需要从串行端口实时绘制一系列 float 。这些值由 '\n' 字符分隔,因此数据序列如下所示: x1 x2 x3 ...

您将如何绘制数据?我使用的是 Arduino 开发板,数据速率为 200 个样本/秒,我的 PC 在 Windows7 64 位上运行。我认为一个不错的选择是使用 pyqtgraph 库。我开始在 pyqtgraph 中使用 Plotting.py 示例(在安装 pyqtgraph 然后运行 ​​python3 -m pyqtgraph.examples 之后可以使用更多示例),但我不知道如何为我的代码调整此代码需要(见下文)。非常感谢您。

from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg

# Set graphical window, its title and size
win = pg.GraphicsWindow(title="Sample process")
win.resize(1000,600)
win.setWindowTitle('pyqtgraph example')

# Enable antialiasing for prettier plots
pg.setConfigOptions(antialias=True)

# Random data process
p6 = win.addPlot(title="Updating plot")
curve = p6.plot(pen='y')
data = np.random.normal(size=(10,1000)) # If the Gaussian distribution shape is, (m, n, k), then m * n * k samples are drawn.

# plot counter
ptr = 0

# Function for updating data display
def update():
global curve, data, ptr, p6
curve.setData(data[ptr%10])
if ptr == 0:
p6.enableAutoRange('xy', False) ## stop auto-scaling after the first data set is plotted
ptr += 1

# Update data display
timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.start(50)


## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()

最佳答案

这是工作正常的代码。主要过程包含在 update() 函数中。它从串行端口读取输入值,更新数组 Xm(包含输入值),然后更新其相关曲线。

发布此代码是为了简单起见,仅适用于低数据速率(低于 100 个样本/秒)。对于更高的数据速率,应在 update() 函数内进行如下修改。应该从串行端口读取一组值(而不是单个值)。然后,这样的集合应该附加到数组 Xm

希望这个回答对您有用,非常感谢您的帮助!

# Import libraries
from numpy import *
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
import serial

# Create object serial port
portName = "COM12" # replace this port name by yours!
baudrate = 9600
ser = serial.Serial(portName,baudrate)

### START QtApp #####
app = QtGui.QApplication([]) # you MUST do this once (initialize things)
####################

win = pg.GraphicsWindow(title="Signal from serial port") # creates a window
p = win.addPlot(title="Realtime plot") # creates empty space for the plot in the window
curve = p.plot() # create an empty "plot" (a curve to plot)

windowWidth = 500 # width of the window displaying the curve
Xm = linspace(0,0,windowWidth) # create array that will contain the relevant time series
ptr = -windowWidth # set first x position

# Realtime data plot. Each time this function is called, the data display is updated
def update():
global curve, ptr, Xm
Xm[:-1] = Xm[1:] # shift data in the temporal mean 1 sample left
value = ser.readline() # read line (single value) from the serial port
Xm[-1] = float(value) # vector containing the instantaneous values
ptr += 1 # update x position for displaying the curve
curve.setData(Xm) # set the curve with this data
curve.setPos(ptr,0) # set x position in the graph to 0
QtGui.QApplication.processEvents() # you MUST process the plot now

### MAIN PROGRAM #####
# this is a brutal infinite loop calling your realtime data plot
while True: update()

### END QtApp ####
pg.QtGui.QApplication.exec_() # you MUST put this at the end
##################

关于使用 pyqtgraph 的 python 实时绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45046239/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com