0

I want to plot 5 graphs on a single canvas. If i click the next button ,it should display the next graph ,and all graphs should be dynamic using 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

1 Answer 1

0

Example with using 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_())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.