-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstandalonematplotlib.py
More file actions
52 lines (37 loc) · 1.6 KB
/
Copy pathstandalonematplotlib.py
File metadata and controls
52 lines (37 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
"""Minimal demo of the matplotlib editor on its own, without the full app.
Run this to check your environment renders a figure and receives mouse events
before debugging anything in ``main.py``:
python standalonematplotlib.py
Drag a box over the curve; the selection coordinates are printed to stdout.
"""
from matplotlib.figure import Figure
from matplotlib.widgets import RectangleSelector
from numpy import cos, linspace, pi, sin
from traits.api import HasTraits, Instance
from traitsui.api import Item, View
from CommonMPL import MPLFigureEditor, MPLInitHandler
class RectangleSelectorDemo(HasTraits):
"""A single figure with a rectangle selector attached to its axes."""
figure = Instance(Figure, ())
view = View(
Item("figure", editor=MPLFigureEditor(), show_label=False),
handler=MPLInitHandler,
resizable=True,
width=640,
height=480,
title="Matplotlib in TraitsUI",
)
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.axes = self.figure.add_subplot(111)
t = linspace(0, 2 * pi, 200)
self.axes.plot(sin(t) * (1 + 0.5 * cos(11 * t)), cos(t) * (1 + 0.5 * cos(11 * t)))
def mpl_setup(self):
"""Called by MPLInitHandler once the canvas exists."""
def onselect(eclick, erelease):
print(f"Start position: {eclick}, End position: {erelease}")
# Held on self so the selector is not garbage collected.
self.rs = RectangleSelector(self.axes, onselect, useblit=True)
if __name__ == "__main__":
RectangleSelectorDemo().configure_traits()