使用 PyQt5 绘制图表

问题描述 投票:0回答:1

我想在一张画布上绘制 5 个图表。如果我单击下一个按钮,它应该显示下一个图形,并且所有图形都应该使用 PyQt5 是动态的

class Window(QWidget):
    def __init__(self):
        super().__init__()
        # Other initialization code...

        # Keep track of the current graph index
        self.current_graph_index = 0

        # Connect next_button to nextGraph method
        next_button.clicked.connect(self.nextGraph)

    def nextGraph(self):
        # Define a list of graph data or functions to plot
        graphs = [
            self.plotGraph,
            self.plotSecondGraph,  # Define another method to plot the second graph
            self.plotThirdGraph    # Define another method to plot the third graph
        ]

        # Increment the current graph index
        self.current_graph_index = (self.current_graph_index + 1) % len(graphs)

        # Clear the canvas
        self.graphWidget1.clear()

        # Call the corresponding method to plot the next graph
        graphs[self.current_graph_index]()

    # Define methods to plot additional graphs if needed
    def plotSecondGraph(self):
        # Plot the second graph
        pass

    def plotThirdGraph(self):
        # Plot the third graph
        pass
pyqt pyqt5 pyqtgraph pyqt6 pyqtdeploy
1个回答
0
投票

使用

pyqtgraph
的示例:

import sys

from PyQt5.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget
import pyqtgraph as pg
import numpy as np


class Window(QWidget):
    def __init__(self):
        super().__init__()

        # Create Layouts
        # --------------
        layout = QVBoxLayout(self)
        
        # Create Widgets
        # --------------
        self.graphWidget1 = pg.GraphicsLayoutWidget()
        next_button = QPushButton("Next Graph")

        # Add Widgets to Layouts
        # ----------------------
        layout.addWidget(self.graphWidget1)
        layout.addWidget(next_button)

        ...

        # Keep track of the current graph index
        self.current_graph_index = 0

        # Connect next_button to nextGraph method
        next_button.clicked.connect(self.nextGraph)

        # Initial plot
        # self.plotGraph()

    def nextGraph(self):
        # Define a list of graph data or functions to plot
        graphs = [
            self.plotGraph,
            self.plotSecondGraph,  # Define another method to plot the second graph
            self.plotThirdGraph    # Define another method to plot the third graph
        ]

        # Increment the current graph index
        self.current_graph_index = (self.current_graph_index + 1) % len(graphs)

        # Clear the canvas
        self.graphWidget1.clear()

        # Call the corresponding method to plot the next graph
        graphs[self.current_graph_index]()

    def plotGraph(self):
        # Example plot
        x = np.random.normal(size=100)
        y = np.random.normal(size=100)
        self.graphWidget1.addPlot().plot(x, y, pen=None, symbol='o')

    # Define methods to plot additional graphs if needed
    def plotSecondGraph(self):
        # Plot the second graph
        x = np.linspace(0, 10, 100)
        y = np.sin(x)
        self.graphWidget1.addPlot().plot(x, y, pen='r')

    def plotThirdGraph(self):
        # Plot the third graph
        x = np.random.normal(size=100)
        y = np.random.normal(size=100)
        self.graphWidget1.addPlot().plot(x, y, pen=None, symbol='x')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = Window()
    win.show()
    sys.exit(app.exec_())
© www.soinside.com 2019 - 2024. All rights reserved.