44===============
55
66Simple Qt application embedding Matplotlib canvases. This program will work
7- equally well using Qt4 and Qt5. Either version of Qt can be selected (for
8- example) by setting the ``MPLBACKEND `` environment variable to "Qt4Agg" or
9- "Qt5Agg" , or by first importing the desired version of PyQt .
7+ equally well using any Qt binding (PyQt6, PySide6, PyQt5, PySide2). The
8+ binding can be selected by setting the ``QT_API `` environment variable to the
9+ binding name , or by first importing it .
1010"""
1111
1212import sys
1313import time
1414
1515import numpy as np
1616
17- from matplotlib .backends .qt_compat import QtCore , QtWidgets
18- from matplotlib .backends .backend_qt5agg import (
17+ from matplotlib .backends .qt_compat import QtWidgets
18+ from matplotlib .backends .backend_qtagg import (
1919 FigureCanvas , NavigationToolbar2QT as NavigationToolbar )
2020from matplotlib .figure import Figure
2121
@@ -28,13 +28,15 @@ def __init__(self):
2828 layout = QtWidgets .QVBoxLayout (self ._main )
2929
3030 static_canvas = FigureCanvas (Figure (figsize = (5 , 3 )))
31+ # Ideally one would use self.addToolBar here, but it is slightly
32+ # incompatible between PyQt6 and other bindings, so we just add the
33+ # toolbar as a plain widget instead.
34+ layout .addWidget (NavigationToolbar (static_canvas , self ))
3135 layout .addWidget (static_canvas )
32- self .addToolBar (NavigationToolbar (static_canvas , self ))
3336
3437 dynamic_canvas = FigureCanvas (Figure (figsize = (5 , 3 )))
3538 layout .addWidget (dynamic_canvas )
36- self .addToolBar (QtCore .Qt .BottomToolBarArea ,
37- NavigationToolbar (dynamic_canvas , self ))
39+ layout .addWidget (NavigationToolbar (dynamic_canvas , self ))
3840
3941 self ._static_ax = static_canvas .figure .subplots ()
4042 t = np .linspace (0 , 10 , 501 )
@@ -66,4 +68,4 @@ def _update_canvas(self):
6668 app .show ()
6769 app .activateWindow ()
6870 app .raise_ ()
69- qapp .exec_ ()
71+ qapp .exec ()
0 commit comments