Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 739ab6e

Browse files
committed
Add test using tool handles
1 parent d05712a commit 739ab6e

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

lib/matplotlib/tests/test_widgets.py

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ def onselect(epress, erelease):
7373
ax._got_onselect = True
7474
assert epress.xdata == 100
7575
assert epress.ydata == 100
76-
assert erelease.xdata == 200
77-
assert erelease.ydata == 200
76+
assert erelease.xdata == 199
77+
assert erelease.ydata == 199
7878

7979
tool = widgets.RectangleSelector(ax, onselect, **kwargs)
8080
event = get_event(ax, xdata=100, ydata=100, button=1)
8181
tool.press(event)
8282

83-
event = get_event(ax, xdata=125, ydata=125, button=1)
83+
event = get_event(ax, xdata=199, ydata=199, button=1)
8484
tool.onmove(event)
8585

8686
# purposely drag outside of axis for release
@@ -99,6 +99,50 @@ def test_rectangle_selector():
9999
check_rectangle(rectprops=dict(fill=True))
100100

101101

102+
def test_rectangle_modifiers():
103+
# TODO: add tests for center, square, center, shift+drag
104+
pass
105+
106+
107+
def test_rectangle_handles():
108+
fig, ax = plt.subplots(1, 1)
109+
ax.plot([0, 200], [0, 200])
110+
ax.figure.canvas.draw()
111+
112+
def onselect(epress, erelease):
113+
pass
114+
115+
tool = widgets.RectangleSelector(ax, onselect=onselect,
116+
maxdist=10)
117+
event = get_event(ax, xdata=100, ydata=100, button=1)
118+
tool.press(event)
119+
120+
tool.extents = (100, 150, 100, 150)
121+
122+
assert tool.corners == (
123+
(100, 150, 150, 100), (100, 100, 150, 150))
124+
assert tool.extents == (100, 150, 100, 150)
125+
assert tool.edge_centers == (
126+
(100, 125.0, 150, 125.0), (125.0, 100, 125.0, 150))
127+
assert tool.extents == (100, 150, 100, 150)
128+
129+
# grab a corner and move it
130+
event = get_event(ax, xdata=100, ydata=100)
131+
tool.press(event)
132+
event = get_event(ax, xdata=120, ydata=120)
133+
tool.onmove(event)
134+
tool.release(event)
135+
assert tool.extents == (120, 150, 120, 150)
136+
137+
# create a new rectangle
138+
event = get_event(ax, xdata=10, ydata=10)
139+
tool.press(event)
140+
event = get_event(ax, xdata=100, ydata=100)
141+
tool.onmove(event)
142+
tool.release(event)
143+
assert tool.extents == (10, 100, 10, 100)
144+
145+
102146
@cleanup
103147
def check_span(*args, **kwargs):
104148
fig, ax = plt.subplots(1, 1)

lib/matplotlib/widgets.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,6 +1509,7 @@ def closest(self, x, y):
15091509
else:
15101510
return 0, np.sqrt(np.sum(diff ** 2))
15111511

1512+
15121513
class RectangleSelector(_SelectorWidget):
15131514
"""
15141515
Select a rectangular region of an axes.
@@ -1638,7 +1639,7 @@ def __init__(self, ax, onselect, drawtype='patch',
16381639
if rectprops is None:
16391640
props = dict(mec='r')
16401641
else:
1641-
props = dict(mec=rectprops['edgecolor'])
1642+
props = dict(mec=rectprops.get('edgecolor', 'k'))
16421643
self._corner_order = ['NW', 'NE', 'SE', 'SW']
16431644
xc, yc = self.corners
16441645
self._corner_handles = ToolHandles(self.ax, xc, yc, marker_props=props,
@@ -1922,12 +1923,6 @@ def _rect_bbox(self):
19221923
y0, y1 = min(y), max(y)
19231924
return x0, y0, x1 - x0, y1 - y0
19241925

1925-
@property
1926-
def geometry(self):
1927-
x0, y0, width, height = self._rect_bbox
1928-
return x0 + width / 2., y0 + width / 2., width, height
1929-
1930-
19311926

19321927
class LassoSelector(_SelectorWidget):
19331928
"""Selection curve of an arbitrary shape.

0 commit comments

Comments
 (0)