当前位置:实例文章 » Python实例» [文章]Python——Pyqt5的数据可视化小工具(完整代码)

Python——Pyqt5的数据可视化小工具(完整代码)

发布人:shili8 发布时间:2023-05-10 14:19 阅读次数:36

Python、Pyqt5、数据可视化、小工具

以下为Python——Pyqt5的数据可视化小工具的完整代码:

```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QAction, QFileDialog, QLabel, QPushButton
from PyQt5.QtGui import QIcon, QPixmap
import pandas as pd
import matplotlib.pyplot as plt

class App(QMainWindow):

def __init__(self):
super().__init__()
self.title = '数据可视化小工具'
self.left = 10
self.top = 10
self.width = 640
self.height = 480
self.initUI()

def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)

# 创建菜单栏
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('文件')

# 添加“打开文件”动作到菜单栏
openFile = QAction(QIcon('open.png'), '打开', self)
openFile.setShortcut('Ctrl+O')
openFile.setStatusTip('打开新文件')
openFile.triggered.connect(self.showDialog)
fileMenu.addAction(openFile)

# 创建标签和按钮
self.label = QLabel(self)
self.label.setGeometry(50, 50, 400, 200)
self.button = QPushButton('绘制图表', self)
self.button.move(50, 300)
self.button.clicked.connect(self.plotGraph)

self.show()

def showDialog(self):
fname = QFileDialog.getOpenFileName(self, '打开文件', '/home')
if fname[0]:
self.df = pd.read_csv(fname[0])
self.label.setText('已打开文件:' + fname[0])
pixmap = QPixmap(fname[0])
self.label.setPixmap(pixmap.scaled(400, 200))

def plotGraph(self):
plt.plot(self.df)
plt.show()

if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
```

参考来源:

相关标签:

免责声明

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱290110527@qq.com删除。

其他信息

其他资源

Top