pyqt5和qtdesigner和pyqtgraph实现读取excel文件并绘制折线图
时间: 2023-05-15 13:06:15 浏览: 243
可以使用 pandas 库来读取 Excel 文件,使用 PyQt5 和 QtDesigner 来设计界面和绘制折线图,使用 PyQtGraph 来显示折线图。以下是一个简单的示例代码:
```python
import sys
import pandas as pd
import pyqtgraph as pg
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.uic import loadUi
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
loadUi('mainwindow.ui', self) # 加载 QtDesigner 设计的界面
self.pushButton.clicked.connect(self.plot) # 绑定按钮点击事件
def plot(self):
filename = self.lineEdit.text() # 获取文件名
try:
df = pd.read_excel(filename) # 读取 Excel 文件
x = df.index.values
y = df.iloc[:, 0].values
self.graphicsView.clear() # 清空图形视图
self.graphicsView.plot(x, y, pen='r') # 绘制折线图
except Exception as e:
print(e)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
```
其中,`mainwindow.ui` 是使用 QtDesigner 设计的界面文件,包含一个 `QLineEdit` 和一个 `QPushButton`,用于输入文件名和触发绘图操作。`graphicsView` 是一个 `PyQtGraph` 的图形视图,用于显示折线图。在 `plot` 方法中,首先获取文件名,然后使用 `pandas` 库读取 Excel 文件,提取数据并绘制折线图。如果出现异常,打印错误信息。最后,创建 `QApplication` 和 `MainWindow` 对象,显示窗口并进入事件循环。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)