Skip to content

Commit 8b94346

Browse files
committed
Add a matplotlib figure editor
1 parent 482082f commit 8b94346

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

ets_tutorial/util/__init__.py

Whitespace-only changes.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import matplotlib
2+
from matplotlib.backends.backend_qt5agg import (
3+
FigureCanvasQTAgg as FigureCanvas,
4+
NavigationToolbar2QT as NavigationToolbar
5+
)
6+
from pyface.qt import QtGui
7+
from traitsui.api import BasicEditorFactory
8+
from traitsui.qt4.editor import Editor
9+
10+
matplotlib.use('Qt5Agg')
11+
12+
13+
class _MplFigureEditor(Editor):
14+
15+
scrollable = True
16+
17+
def init(self, parent):
18+
self.set_tooltip()
19+
self.control = self._create_mpl_canvas(parent)
20+
21+
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()
29+
layout.addWidget(toolbar)
30+
layout.addWidget(canvas)
31+
control.setLayout(layout)
32+
return control
33+
34+
35+
class MplFigureEditor(BasicEditorFactory):
36+
klass = _MplFigureEditor

ets_tutorial/util/tests/__init__.py

Whitespace-only changes.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import unittest
2+
3+
import numpy as np
4+
from matplotlib.figure import Figure
5+
from traits.api import HasTraits, Instance
6+
from traitsui.api import Item, View
7+
from traitsui.testing.api import UITester, IsVisible
8+
9+
from ets_tutorial.util.mpl_figure_editor import MplFigureEditor
10+
11+
12+
class Plot(HasTraits):
13+
figure = Instance(Figure, ())
14+
15+
view = View(
16+
Item("figure", editor=MplFigureEditor(), show_label=False),
17+
)
18+
19+
def _figure_default(self):
20+
figure = Figure()
21+
axes = figure.add_subplot(111)
22+
time = np.linspace(0, 2 * np.pi, 200)
23+
axes.plot(
24+
np.sin(time) * (1 + 0.5 * np.cos(11 * time)),
25+
np.cos(time) * (1 + 0.5 * np.cos(11 * time)),
26+
)
27+
return figure
28+
29+
30+
class TestMplFigureEditor(unittest.TestCase):
31+
def test_mpl_figure_editor(self):
32+
tester = UITester()
33+
plot = Plot()
34+
with tester.create_ui(plot) as ui:
35+
# smoke test: just check if Plot can create its UI
36+
figure = tester.find_by_name(ui, "figure")
37+
figure.inspect(IsVisible())

0 commit comments

Comments
 (0)