Many times it is required to embed matplotlib with Qt since it allows to use many more types of widgets or to use the other Qt modules. And fortunately matplotlib provides Qt as a backend, but unfortunately the official examples (see official example here) are limited so this post will try to provide examples and basic tips on how both libraries should interact.
The following rules are basic:
-
The binding (PyQt5 or PySide2) must be imported first so that matplotlib that library must use internally.
-
You should not use
matplotlib.pyplot
butFigure
to manipulate the painting and the FigureCanvas as a widget.
Many functions or classes accept an AxesSubplot
as a parameter (with the argument "ax") so it must be used to point to the axis where it should be drawn.
In this section I show the translation of the official examples of the matplotlib widgets from matplotlib.pyplot
to Qt.