有一个不断生成的数据,随着时间将该这个数据用pyqtgragh绘制UI界面,并让横坐标轴跟着更新移动
时间: 2023-07-15 11:13:06 浏览: 49
可以使用QTimer和pyqtgraph的PlotWidget来实现该功能。具体实现步骤如下:
1. 创建一个Qt应用程序,导入需要的模块。
```python
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import QTimer
import pyqtgraph as pg
import numpy as np
```
2. 创建一个主窗口,并在窗口中添加一个PlotWidget。
```python
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# 创建一个PlotWidget
self.plot_widget = pg.PlotWidget()
self.setCentralWidget(self.plot_widget)
```
3. 创建一个定时器,用于定时更新数据和移动横坐标轴。
```python
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# 创建一个PlotWidget
self.plot_widget = pg.PlotWidget()
self.setCentralWidget(self.plot_widget)
# 创建一个定时器
self.timer = QTimer()
self.timer.timeout.connect(self.update_plot)
self.timer.start(50) # 50ms更新一次
```
4. 在定时器的槽函数中更新数据,并将数据添加到PlotWidget中,并移动横坐标轴。
```python
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# 创建一个PlotWidget
self.plot_widget = pg.PlotWidget()
self.setCentralWidget(self.plot_widget)
# 创建一个定时器
self.timer = QTimer()
self.timer.timeout.connect(self.update_plot)
self.timer.start(50) # 50ms更新一次
# 初始化数据
self.x_data = np.arange(100)
self.y_data = np.random.rand(100)
def update_plot(self):
# 更新数据
self.y_data[:-1] = self.y_data[1:]
self.y_data[-1] = np.random.rand()
# 将数据添加到PlotWidget中
self.plot_widget.plot(self.x_data, self.y_data)
# 移动横坐标轴
self.plot_widget.setXRange(self.x_data[0], self.x_data[-1])
```
5. 运行程序。
```python
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
```
完整代码如下:
```python
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import QTimer
import pyqtgraph as pg
import numpy as np
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# 创建一个PlotWidget
self.plot_widget = pg.PlotWidget()
self.setCentralWidget(self.plot_widget)
# 创建一个定时器
self.timer = QTimer()
self.timer.timeout.connect(self.update_plot)
self.timer.start(50) # 50ms更新一次
# 初始化数据
self.x_data = np.arange(100)
self.y_data = np.random.rand(100)
def update_plot(self):
# 更新数据
self.y_data[:-1] = self.y_data[1:]
self.y_data[-1] = np.random.rand()
# 将数据添加到PlotWidget中
self.plot_widget.plot(self.x_data, self.y_data)
# 移动横坐标轴
self.plot_widget.setXRange(self.x_data[0], self.x_data[-1])
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)