python logo

pyqt5 matplotlib


Python hosting: Host, run, and code Python in the cloud!

Matplotlib offers powerful visualizations that can be seamlessly integrated into a PyQt5 application. For this, specific libraries and imports are required.

Here’s how you can include Matplotlib plots within a PyQt5 application:

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt

The primary component here is a widget named ‘PlotCanvas’ which houses the Matplotlib visualization.

Matplot plot in a PyQt5 window

Integration of Matplotlib with PyQt5
The example provided below illustrates the embedding process of a Matplotlib plot within a PyQt5 window. Additionally, we’ll integrate a qpushbutton for demonstration.

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu, QVBoxLayout, QSizePolicy, QMessageBox, QWidget, QPushButton
from PyQt5.QtGui import QIcon
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import random

class App(QMainWindow):

def __init__(self):
super().__init__()
self.left = 10
self.top = 10
self.title = 'PyQt5 and Matplotlib Integration - pythonspot.com'
self.width = 640
self.height = 400
self.initUI()

def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
m = PlotCanvas(self, width=5, height=4)
m.move(0,0)
button = QPushButton('Sample PyQt5 Button', self)
button.setToolTip('This is an illustrative button')
button.move(500,0)
button.resize(140,100)
self.show()

class PlotCanvas(FigureCanvas):

def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
FigureCanvas.__init__(self, fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self, QSizePolicy.Expanding, QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
self.plot()

def plot(self):
data = [random.random() for i in range(25)]
ax = self.figure.add_subplot(111)
ax.plot(data, 'r-')
ax.set_title('PyQt and Matplotlib Demonstration')
self.draw()

if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())

For those keen on diving deeper into PyQt5’s capabilities, consider downloading these comprehensive PyQT5 Example Codes.

Navigation: Previous | Next






Leave a Reply:




hasjalilg . Sun, 12 Feb 2017

The example works well if we change the import instructions as:


import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu, QVBoxLayout, QSizePolicy, QMessageBox, QWidget, QPushButton
from PyQt5.QtGui import QIcon
#******************************************************************
import matplotlib
matplotlib.use("Qt5Agg")
#******************************************************************
from PyQt5 import QtCore
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import random
#******************************************************************

Stuart Walsh Wed, 14 Jun 2017

Hmm, it doesn't work. Comes up with this problem:

ImportError: No module named 'matplotlib'

Does anyone have a fix for this please?

Stuart

Frank Thu, 15 Jun 2017

The module matplotlib is missing.
Installation instructions for this module can be found here: https://matplotlib.org/users/installing.html

The Ubuntourist 2021-07-06T20:21:03.597Z

In the matplotlib example (https://pythonspot.com/pyqt5-matplotlib/):

'MatplotlibDeprecationWarning: Adding an axes using the same arguments as a
previous axes currently reuses the earlier instance. In a future version, a new
instance will always be created and returned. Meanwhile, this warning can be
suppressed, and the future behavior ensured, by passing a unique label to each
axes instance.'
ax = self.figure.add_subplot(111)
This can be fixed by removing the 'self.axes' line from the __init__().