利用pyqt5的pyqtgraph绘制不断更新的数值曲线
时间: 2023-06-29 15:08:07 浏览: 177
PyQt5的PyQtGraph实践系列3之实时数据更新绘制图形
5星 · 资源好评率100%
可以通过以下步骤使用 pyqtgraph 绘制不断更新的数值曲线:
1. 安装 pyqtgraph 库
可以通过 pip 命令安装 pyqtgraph:
```
pip install pyqtgraph
```
2. 导入必要的库
```python
import pyqtgraph as pg
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import QTimer
import numpy as np
```
3. 创建 PyQt5 应用程序和主窗口
```python
app = QApplication([])
win = QMainWindow()
```
4. 创建 pyqtgraph 组件
```python
plot = pg.PlotWidget()
win.setCentralWidget(plot)
```
5. 定义更新数据的函数
```python
def update_data():
# 生成新的数据
data = np.random.normal(size=(100,))
# 更新数据
curve.setData(data)
```
6. 创建定时器
```python
timer = QTimer()
timer.timeout.connect(update_data)
timer.start(50) # 50 毫秒更新一次数据
```
7. 绘制曲线
```python
data = np.random.normal(size=(100,))
curve = plot.plot(data)
```
8. 显示窗口
```python
win.show()
app.exec_()
```
完整代码:
```python
import pyqtgraph as pg
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import QTimer
import numpy as np
app = QApplication([])
win = QMainWindow()
plot = pg.PlotWidget()
win.setCentralWidget(plot)
def update_data():
data = np.random.normal(size=(100,))
curve.setData(data)
timer = QTimer()
timer.timeout.connect(update_data)
timer.start(50)
data = np.random.normal(size=(100,))
curve = plot.plot(data)
win.show()
app.exec_()
```
阅读全文