In this article we want to talk about Python PyQtGraph: A Guide to Dynamic and Interactive Visualizations.
Introduction
Python is powerful and popular programming language that is widely used for scientific computing, data analysis and web development. when it comes to visualizing data, Python has different library and packages to choose from, including PyQtGraph.
PyQtGraph is powerful and easy-to-use graphics library for Python that allows you to create dynamic and interactive visualizations for your data. in this article we are going to explore PyQtGraph and how you can use it to create visualizations for your data.
What is PyQtGraph ?
PyQtGraph is graphics library for Python and it is built on top of PyQt library. it is designed to be fast and efficient and also it allows you to create dynamic and interactive visualizations with less code. PyQtGraph supports different plotting and visualization styles such as 2D and 3D plotting and allows you to customize the appearance of your visualizations.
Why use PyQtGraph ?
There are many reasons why you might choose PyQtGraph for your data visualization needs. These are some reasons:
- Ease of use: PyQtGraph has simple and intuitive API that makes it easy to create visualizations for your data.
- Speed and performance: PyQtGraph is designed to be fast and efficient, allowing you to create visualizations in real-time even for large datasets.
- Customization: PyQtGraph allows you to customize the appearance of your visualizations to meet your specific needs.
- Interactivity: PyQtGraph supports interactive visualizations, allowing you to explore your data in real-time and gain new insights.
Getting started with PyQtGraph
Getting started with PyQtGraph is simple . you can install PyQtGraph using pip:
1 |
pip install PyQtGraph |
After you have installed PyQtGraph, you can start creating visualizations for your data. this is simple example of how you can use PyQtGraph to create a line plot:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import pyqtgraph as pg import numpy as np import sys from PyQt6 import QtWidgets x = np.linspace(0, 10, 100) y = np.sin(x) # Create a plot window plot_window = pg.plot() # Add a line plot plot_window.plot(x, y) # Show the plot window if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) plot_window.show() sys.exit(app.exec()) |
In this example we have used linspace
function from the NumPy library to generate an array of 100 points between 0 and 10, after that we use sin
function to generate the y-values for our plot.
Next we create plot window using the plot
function from PyQtGraph, and add line plot to the window using the plot
function. Finally, we show the plot window using show
function. the if __name__ == '__main__':
block starts Qt event loop and runs the app.exec()
method, which is necessary to display the plot window. the event loop is what allows the window to be updated and interact with the user.
Run the complete code and this will be the result.

Here are some more examples to help you understand the capabilities of PyQtGraph.
Scatter Plot:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout import sys import pyqtgraph as pg import numpy as np class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("PyQtGraph Scatter") # Generate random data x = np.random.normal(0, 1, 100) y = np.random.normal(0, 1, 100) self.pw = pg.PlotWidget() self.plot = self.pw.plot(x, y, pen=None, symbol='o', symbolBursh='r') vbox = QVBoxLayout() vbox.addWidget(self.pw) self.setLayout(vbox) app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec()) |
Run the complete code and this will be the result.

Bar Plot:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
from PyQt6.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton import sys import pyqtgraph as pg import numpy as np class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("PyQtGraph BarGraph") hbox = QHBoxLayout() win = pg.PlotWidget() x = np.arange(10) y1 = np.sin(x) y2 = 1.1 * np.sin(x+1) y3 = 1.2 * np.sin(x+2) bg1 = pg.BarGraphItem(x=x, height = y1, width=0.3, brush='r') bg2 = pg.BarGraphItem(x=x+0.33, height=y2, width=0.3, brush='g') bg3 = pg.BarGraphItem(x=x + 0.66, height=y3, width=0.3, brush='b') btn = QPushButton("Click Me") win.addItem(bg1) win.addItem(bg2) win.addItem(bg3) hbox.addWidget(win) hbox.addWidget(btn) self.setLayout(hbox) app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec()) |
Run the complete code and this will be the result.

These examples demonstrate different visualizations that you can create using PyQtGraph. you can use PyQtGraph to create simple line plot or complex 3D surface plot, PyQtGraph has you all things. it has simple API and customizable appearance, PyQtGraph is valuable tool for any data scientist or analyst.
-
Learn More on Python
- Get Started with wxPython: A Complete Guide to Building GUI Applications
- Python: The Most Versatile Programming Language of the 21st Century
- Tkinter: A Beginner’s Guide to Building GUI Applications in Python
- PySide6: The Cross-Platform GUI Framework for Python
- The Ultimate Guide to Kivy: Building Cross-Platform Apps with Python
- Discover the Power of Django: The Best Web Framework for Your Next Project
- How to Earn Money with Python
- Why Flask is the Ideal Micro-Web Framework
- Python Pillow: The Ultimate Guide to Image Processing with Python
- Get Started with Pygame: A Beginner’s Guide to Game Development with Python
- Python PyOpenGL: A Guide to High-Performance 3D Graphics in Python
- The Cross-Platform Game Development Library in Python
- Unleash the Power of Computer Vision with Python OpenCV
- PyQt6 Charts: An Overview and its Importance in Data Visualization
- Maximizing Your Productivity with Python and Excel