11import matplotlib
22from matplotlib .backends .backend_qt5agg import (
3- FigureCanvasQTAgg as FigureCanvas ,
4- NavigationToolbar2QT as NavigationToolbar
3+ FigureCanvasQTAgg , NavigationToolbar2QT
54)
65from pyface .qt import QtGui
76from traitsui .api import BasicEditorFactory
@@ -15,21 +14,32 @@ class _MplFigureEditor(Editor):
1514 scrollable = True
1615
1716 def init (self , parent ):
17+ """Create and initialize the underlying toolkit widget.
18+ """
1819 self .set_tooltip ()
19- self .control = self ._create_mpl_canvas (parent )
20+ self .control = QtGui .QWidget ()
21+ self .control .setLayout (QtGui .QVBoxLayout ())
22+ self ._do_layout ()
2023
2124 def update_editor (self ):
22- pass
23-
24- def _create_mpl_canvas (self , parent ):
25- control = QtGui .QWidget ()
26- canvas = FigureCanvas (figure = self .value )
27- toolbar = NavigationToolbar (canvas , control )
28- layout = QtGui .QVBoxLayout ()
25+ """Updates the editor when the value changes externally to the editor.
26+ """
27+ self .clear_layout ()
28+ self ._do_layout ()
29+
30+ def _do_layout (self ):
31+ """Creates sub-widgets and does layout.
32+ """
33+ canvas = FigureCanvasQTAgg (figure = self .value )
34+ # Allow the figure canvas to expand and shrink with the main widget.
35+ canvas .setSizePolicy (
36+ QtGui .QSizePolicy .Policy .Expanding ,
37+ QtGui .QSizePolicy .Policy .Expanding ,
38+ )
39+ toolbar = NavigationToolbar2QT (canvas , self .control )
40+ layout = self .control .layout ()
2941 layout .addWidget (toolbar )
3042 layout .addWidget (canvas )
31- control .setLayout (layout )
32- return control
3343
3444
3545class MplFigureEditor (BasicEditorFactory ):
0 commit comments