Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Hai



  • I'm working on pyqt. I couldn't display series but I,m able to print x-axis(QDateTimeAxis from already existing list) and y axis in Pyqt using pyqtgraph. I reading x and y values from csv files.
    //here con_tmp[i] is a column from csv file which contains timestamp values
    //col is a also a column from csv files which consists data

    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(800, 600)
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.verticalScrollBar = QtWidgets.QScrollBar(self.centralwidget)
            self.verticalScrollBar.setGeometry(QtCore.QRect(770, 0, 16, 861))
            self.verticalScrollBar.setOrientation(QtCore.Qt.Vertical)
            self.verticalScrollBar.setObjectName("verticalScrollBar")
            self.scrollArea = QtWidgets.QScrollArea(self.centralwidget)
            self.scrollArea.setGeometry(QtCore.QRect(-1, -1, 771, 611))
            self.scrollArea.setWidgetResizable(True)
            self.scrollArea.setObjectName("scrollArea")
            self.scrollAreaWidgetContents = QtWidgets.QWidget()
            self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 769, 609))
            self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
            self.widget = QtWidgets.QWidget(self.scrollAreaWidgetContents)
            self.widget.setGeometry(QtCore.QRect(39, 29, 701, 341))
            self.widget.setObjectName("widget")
            self.widget_2 = QtWidgets.QWidget(self.scrollAreaWidgetContents)
            self.widget_2.setGeometry(QtCore.QRect(40, 380, 701, 271))
            self.widget_2.setObjectName("widget_2")
            self.scrollArea.setWidget(self.scrollAreaWidgetContents)
            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QtWidgets.QMenuBar(MainWindow)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
            self.menubar.setObjectName("menubar")
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QtWidgets.QStatusBar(MainWindow)
            self.statusbar.setObjectName("statusbar")
            MainWindow.setStatusBar(self.statusbar)
    
            ##-------------------------------------------------------------------------------------------#        
            date_list_qt=list()
            for i in range(len(df)):
                timestamp=con_tmp[i]
                # print(timestamp)
                startTime = QDateTime()
                startTime = QDateTime.fromString(timestamp, "yyyy-MM-dd hh:mm:ss")
                print(startTime)
                date_list_qt.append(startTime)
            ##-------------------------------------------------------------------------------------------#
            series = QLineSeries()
            chart = QChart()
            for i in range(len(df)):
            # Getting the data
                date_fmt = "HH:mm:s"
    
                # x = QDateTime().fromString(date_list[i], date_fmt).toSecsSinceEpoch()
                x = float(date_list_qt[i].toSecsSinceEpoch())
                y = col[i] 
                #series.append(x,y)
                p=QPoint() 
                p.setX(p.x() + x)
                p = QPointF(x,y)
                print(p)        
                series.append(p)
    
            chart.addSeries(series)
            chart.setAnimationOptions(QChart.SeriesAnimations)
            chart.setTitle("Line Chart Example")
            chart.setTheme(QChart.ChartThemeBlueCerulean)
             ##-------------------------------------------------------------------------------------------#
            axisX = QDateTimeAxis()
            axisX.setTickCount(5)
            axisX.setFormat("HH:mm:ss")
            axisX.setTitleText("Date")
            yt=date_list_qt[0]
            axisX.setMin(yt)
            axisX.setMax(date_list_qt[len(df)-1])
            chart.addAxis(axisX, Qt.AlignBottom)
            series.attachAxis(axisX)
            ##-------------------------------------------------------------------------------------------#
            axis_y= QValueAxis()
            axis_y.setMax(col[0])
            axis_y.setTickCount(5)
            axis_y.setMin(col[len(df)-1])
            chart.addAxis(axis_y, Qt.AlignLeft)
            axis_y.setTitleText("Barometer")
            series.attachAxis(axis_y)
            ##-------------------------------------------------------------------------------------------#
            chartview = QChartView(chart)
            # chartview.chart().setAxisX(axisX, series)
            
     
            vbox = QVBoxLayout()
            vbox.addWidget(chartview)
            
            self.widget.setLayout(vbox)
            
            ##-------------------------------------------------------------------------------------------#
            self.retranslateUi(MainWindow)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
        def retranslateUi(self, MainWindow):
            _translate = QtCore.QCoreApplication.translate
            MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
    
    
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        MainWindow = QtWidgets.QMainWindow()
        ui = Ui_MainWindow()
        ui.setupUi(MainWindow)
        MainWindow.show()
        sys.exit(app.exec_())

Log in to reply