PyQt Real Time Graph

Posted by


In this tutorial, we will be creating a real-time graph using PyQt and Matplotlib in Python. The real-time graph will continuously update with new data points, allowing us to visualize data as it comes in real time.

Step 1: Install dependencies
Before we start, make sure you have PyQt and Matplotlib installed in your Python environment. You can install them using pip by running the following commands:

pip install PyQt5
pip install matplotlib

Step 2: Create a PyQt application
First, let’s create a PyQt application that will contain our real-time graph. Create a new Python file (e.g., real_time_graph.py) and import the necessary modules:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure

Next, create a class that inherits from QWidget and add a constructor method that initializes the UI:

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

        # Initialize the UI
        self.initUI()

    def initUI(self):
        self.setWindowTitle('Real Time Graph')
        self.setGeometry(100, 100, 800, 600)

        self.layout = QVBoxLayout()
        self.setLayout(self.layout)

        # Create a Matplotlib figure
        self.figure = Figure()
        self.canvas = FigureCanvas(self.figure)
        self.layout.addWidget(self.canvas)

Step 3: Create a Matplotlib subplot
Now, let’s add a subplot to our Matplotlib figure that will display the real-time data. In the initUI method of the RealTimeGraph class, add the following code:

        # Add a subplot to the figure
        self.subplot = self.figure.add_subplot(111)
        self.line, = self.subplot.plot([], [], '-o')

Step 4: Update the graph in real time
To update the graph in real time, we will create a method called update_graph that will be called periodically to add new data points to the graph. Here’s the complete RealTimeGraph class with the update_graph method:

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

        # Initialize the UI
        self.initUI()

    def initUI(self):
        self.setWindowTitle('Real Time Graph')
        self.setGeometry(100, 100, 800, 600)

        self.layout = QVBoxLayout()
        self.setLayout(self.layout)

        # Create a Matplotlib figure
        self.figure = Figure()
        self.canvas = FigureCanvas(self.figure)
        self.layout.addWidget(self.canvas)

        # Add a subplot to the figure
        self.subplot = self.figure.add_subplot(111)
        self.line, = self.subplot.plot([], [], '-o')

    def update_graph(self, x, y):
        self.line.set_xdata(x)
        self.line.set_ydata(y)
        self.subplot.relim()
        self.subplot.autoscale_view()
        self.canvas.draw()

Step 5: Create a real-time data source
To test our real-time graph, we need to create a data source that generates new data points periodically. For this tutorial, we will use a simple sine wave as our data source. Add the following code at the end of the real_time_graph.py file:

import numpy as np
import time

if __name__ == '__main__':
    app = QApplication(sys.argv)

    graph = RealTimeGraph()
    graph.show()

    x = np.linspace(0, 10, 100)
    y = np.sin(x)

    while True:
        graph.update_graph(x, y)
        app.processEvents()
        time.sleep(0.1)

Step 6: Run the application
Now that we have completed the implementation, you can run the application by executing the real_time_graph.py file in your Python environment. You should see a window showing a real-time sine wave graph that updates every 0.1 seconds.

In this tutorial, we have created a real-time graph using PyQt and Matplotlib in Python. You can customize the graph by adding more data points, changing the data source, or adjusting the plot parameters. Feel free to experiment with different configurations to create real-time graphs that visualize your data in real time.

0 0 votes
Article Rating

Leave a Reply

4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@devran4169
2 hours ago

<3

@matzeherd1343
2 hours ago

Pretty impressive 😅
While i am struggling to plot 4 Graphs with 30fps 😕

@SilentShiba
2 hours ago

Yeah bro, do you have a repo? I would love to see your implementation

@drpaprikaa
2 hours ago

Hi, is there a Github repo where we can see the code ? Thanks !

4
0
Would love your thoughts, please comment.x
()
x