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

Skip to content

Commit d8ed099

Browse files
committed
Allow Rectangle and Ellipse selectors to be rotated
1 parent 876415d commit d8ed099

File tree

3 files changed

+200
-72
lines changed

3 files changed

+200
-72
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Rotating selectors
2+
------------------
3+
4+
The `~matplotlib.widgets.RectangleSelector` and
5+
`~matplotlib.widgets.EllipseSelector` can now be rotated by pressing the 'r' key,
6+
and dragging one of their control points. The rotation is done about the first
7+
vertex placed when drawing the selector.

lib/matplotlib/tests/test_widgets.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,43 @@ def onselect(epress, erelease):
189189
tool._corner_handles.artist.get_markeredgecolor(), 'b')
190190

191191

192+
@pytest.mark.parametrize('selector_class', [widgets.RectangleSelector,
193+
widgets.EllipseSelector])
194+
def test_rectangle_rotate(selector_class):
195+
ax = get_ax()
196+
197+
def onselect(epress, erelease):
198+
pass
199+
200+
tool = selector_class(ax, onselect=onselect, interactive=True)
201+
# Draw rectangle
202+
do_event(tool, 'press', xdata=100, ydata=100)
203+
do_event(tool, 'onmove', xdata=130, ydata=140)
204+
do_event(tool, 'release', xdata=130, ydata=140)
205+
assert tool.extents == (100, 130, 100, 140)
206+
207+
# Rotate anticlockwise using top-right corner
208+
do_event(tool, 'on_key_press', key='r')
209+
do_event(tool, 'press', xdata=130, ydata=140)
210+
do_event(tool, 'onmove', xdata=100, ydata=150)
211+
do_event(tool, 'release', xdata=100, ydata=200)
212+
do_event(tool, 'on_key_release', key='r')
213+
# Extents shouldn't change (as shape of rectangle hasn't changed)
214+
assert tool.extents == (100, 130, 100, 140)
215+
# Corners should move
216+
# The third corner is at (100, 150), as the diagonal of the rectangle has
217+
# length 50 (it is a 3,4,5 scaled right angled triangle)
218+
assert tool.corners == ((100, 124, 100, 76), (100, 118, 150, 132))
219+
220+
# Scale using top-right corner
221+
do_event(tool, 'press', xdata=100, ydata=150)
222+
do_event(tool, 'onmove', xdata=100, ydata=125)
223+
do_event(tool, 'release', xdata=100, ydata=125)
224+
assert tool.extents == (100, 115, 100, 120)
225+
# The bottom-left corner doesn't move, the top-right moves to (100, 125)
226+
assert tool.corners == ((100, 112, 100, 88), (100, 109, 125, 116))
227+
228+
192229
def check_span(*args, **kwargs):
193230
ax = get_ax()
194231

0 commit comments

Comments
 (0)