Skip to content

Commit c2169d0

Browse files
Merge pull request #43 from jonathanrocher/enh/update_mpl_editor
Improve the matplotlib figure editor
2 parents 0829be5 + 569a1a6 commit c2169d0

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

ets_tutorial/util/mpl_figure_editor.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import matplotlib
22
from matplotlib.backends.backend_qt5agg import (
3-
FigureCanvasQTAgg as FigureCanvas,
4-
NavigationToolbar2QT as NavigationToolbar
3+
FigureCanvasQTAgg, NavigationToolbar2QT
54
)
65
from pyface.qt import QtGui
76
from 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

3545
class MplFigureEditor(BasicEditorFactory):

ets_tutorial/util/tests/test_mpl_figure_editor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Plot(HasTraits):
1414

1515
view = View(
1616
Item("figure", editor=MplFigureEditor(), show_label=False),
17+
resizable=True
1718
)
1819

1920
def _figure_default(self):

0 commit comments

Comments
 (0)