Python——作业12(选做)选中矩阵的每行或每列画出对应的折线图(python programming)

Python——作业12(选做)选中矩阵的每行或每列画出对应的折线图(python programming)Python——作业12(选做)选中矩阵的每行或每列画出对应的折线图(python programming)

import os
import platform
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import * import numpy as np
import matplotlib
matplotlib.use("Qt5Agg")
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure #注意不是pyplot下的那个figure class Table(QWidget): def __init__(self):
super().__init__()
hhbox = QHBoxLayout() #横向布局 self.tableWidget = QTableWidget() #创建一个表格
self.tableWidget.setRowCount(5)
self.tableWidget.setColumnCount(4) #5行4列
self.tableWidget.setHorizontalHeaderLabels(['第一行列','第二列','第三列','第四列'])
self.tableWidget.setVerticalHeaderLabels(['第一行','第二行','第三行','第四行','第五行'])
#表头 # 往表格里添加东西
#nu self.tableWidget.setItem(0,0,QTableWidgetItem(""))
self.tableWidget.setItem(0,1,QTableWidgetItem(""))
self.tableWidget.setItem(0,2,QTableWidgetItem(""))
self.tableWidget.setItem(0,3,QTableWidgetItem("")) self.tableWidget.setItem(1,0,QTableWidgetItem(""))
self.tableWidget.setItem(1,1,QTableWidgetItem(""))
self.tableWidget.setItem(1,2,QTableWidgetItem(""))
self.tableWidget.setItem(1,3,QTableWidgetItem("")) self.tableWidget.setItem(2,0,QTableWidgetItem(""))
self.tableWidget.setItem(2,1,QTableWidgetItem(""))
self.tableWidget.setItem(2,2,QTableWidgetItem(""))
self.tableWidget.setItem(2,3,QTableWidgetItem("")) self.tableWidget.setItem(3,0,QTableWidgetItem(""))
self.tableWidget.setItem(3,1,QTableWidgetItem(""))
self.tableWidget.setItem(3,2,QTableWidgetItem(""))
self.tableWidget.setItem(3,3,QTableWidgetItem("")) self.tableWidget.setItem(4,0,QTableWidgetItem(""))
self.tableWidget.setItem(4,1,QTableWidgetItem(""))
self.tableWidget.setItem(4,2,QTableWidgetItem(""))
self.tableWidget.setItem(4,3,QTableWidgetItem("")) hhbox.addWidget(self.tableWidget) #把表格加入布局 self.setLayout(hhbox) #创建布局 self.setWindowTitle("表格")
self.resize(600,250)
self.show() class Draw(QDialog):
def __init__(self,parent=None):
super().__init__(parent)
self.table = Table()
self.ok = QPushButton()
self.ok.setText('画图') #绘图
self.fig = Figure(figsize=(3, 3), dpi=100)
self.figureCanvas = FigureCanvas(self.fig)
self.graphicscene = QGraphicsScene()
self.graphicscene.addWidget(self.figureCanvas)
self.graphicview = QGraphicsView()
self.graphicview.setScene(self.graphicscene)
#self.setCentralWidget(self.graphicview) layout = QGridLayout()
layout.addWidget(self.table, 0, 0)
layout.addWidget(self.ok, 1, 0)
layout.addWidget(self.graphicview, 0, 1)
self.setLayout(layout) self.ok.clicked.connect(self.draw_pic) def draw_pic(self): #获取选中的项目
item = self.table.tableWidget.selectedItems()
ls=[]
'''
for i in item:
ls.extend(self.table.tableWidget.indexFromItem(i).data())
print(self.table.tableWidget.indexFromItem(i).data())
''' for i in item:
a=self.table.tableWidget.indexFromItem(i).row()
#print(a)
#print(dir(self.table.tableWidget.indexFromItem(i)))
b=self.table.tableWidget.indexFromItem(i).column()
#print(b)
#print(self.table.tableWidget.indexFromItem(i).data())
ls.append(int(self.table.tableWidget.item(a,b).text())) print(ls) #显示
self.fig.clf()
self.ax = self.fig.add_subplot(1,1,1)
self.ax.plot(ls)
self.figureCanvas.draw()
pass if __name__ == "__main__":
'''
app = QApplication(sys.argv)
dlg = Example()
sys.exit(app.exec_())
'''
app = QApplication(sys.argv)
form = Draw()
form.setMinimumSize(1000,400)
form.show()
app.exec_()
上一篇:mongodb入门教程二


下一篇:springboot+dubbo之多端口注入服务