• 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("1"))
            self.tableWidget.setItem(0,1,QTableWidgetItem("2"))
            self.tableWidget.setItem(0,2,QTableWidgetItem("3"))
            self.tableWidget.setItem(0,3,QTableWidgetItem("3"))
            
            self.tableWidget.setItem(1,0,QTableWidgetItem("7"))
            self.tableWidget.setItem(1,1,QTableWidgetItem("5"))
            self.tableWidget.setItem(1,2,QTableWidgetItem("6"))
            self.tableWidget.setItem(1,3,QTableWidgetItem("7"))
    
            self.tableWidget.setItem(2,0,QTableWidgetItem("8"))
            self.tableWidget.setItem(2,1,QTableWidgetItem("9"))
            self.tableWidget.setItem(2,2,QTableWidgetItem("10"))
            self.tableWidget.setItem(2,3,QTableWidgetItem("11"))
    
            self.tableWidget.setItem(3,0,QTableWidgetItem("4"))
            self.tableWidget.setItem(3,1,QTableWidgetItem("2"))
            self.tableWidget.setItem(3,2,QTableWidgetItem("3"))
            self.tableWidget.setItem(3,3,QTableWidgetItem("1"))
    
            self.tableWidget.setItem(4,0,QTableWidgetItem("8"))
            self.tableWidget.setItem(4,1,QTableWidgetItem("9"))
            self.tableWidget.setItem(4,2,QTableWidgetItem("10"))
            self.tableWidget.setItem(4,3,QTableWidgetItem("5"))
            
    
            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_()
  • 相关阅读:
    Windows系统下静态库和动态库的生成方法
    c语言 9-9
    c语言中统计字符串中数字字符出现的次数
    c语言 9-8
    c语言 9-7
    c语言中使用putchar显示字符串
    c语言 9-6
    c语言 9-5
    c语言 9-4
    c语言中输出字符串的长度
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/10968595.html
Copyright © 2020-2023  润新知