Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Improve RectangleSelector Rotation revisited and rebased#26833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
dhomeier wants to merge9 commits intomatplotlib:main
base:main
Choose a base branch
Loading
fromdhomeier:selector-rotation
Open
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
Rectangle resize tests with multiple start/end corners
Co-authored-by: David Stansby <dstansby@gmail.com>
  • Loading branch information
@dhomeier@dstansby
dhomeier anddstansby committedJan 28, 2025
commit78d0f0231871c88270a975363902d246ff60d77d
109 changes: 65 additions & 44 deletionslib/matplotlib/tests/test_widgets.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -82,19 +82,20 @@ def test_rectangle_selector(ax, kwargs):

# purposely drag outside of axis for release
do_event(tool, 'release', xdata=250, ydata=250, button=1)
do_event(tool, 'onmove', xdata=250, ydata=250, button=1)

if kwargs.get('drawtype', None) not in ['line', 'none']:
assert_allclose(tool.geometry,
[[100., 100, 199, 199, 100],
[[100, 100, 199, 199, 100],
[100, 199, 199, 100, 100]],
err_msg=tool.geometry)

onselect.assert_called_once()
(epress, erelease), kwargs = onselect.call_args
assert epress.xdata == 100
assert epress.ydata == 100
assert erelease.xdata ==199
assert erelease.ydata ==199
assert erelease.xdata ==200
assert erelease.ydata ==200
assert kwargs == {}


Expand DownExpand Up@@ -181,10 +182,16 @@ def test_rectangle_selector_set_props_handle_props(ax):
assert artist.get_alpha() == 0.3


def test_rectangle_resize(ax):
# Should give same results if rectangle is created from any two
# opposite corners
@pytest.mark.parametrize('start, end', [[(0, 10), (100, 120)],
[(100, 120), (0, 10)],
[(0, 120), (100, 10)],
[(100, 10), (0, 120)]])
def test_rectangle_resize(ax, start, end):
tool = widgets.RectangleSelector(ax, interactive=True)
# Create rectangle
click_and_drag(tool, start=(0, 10),end=(100, 120))
click_and_drag(tool, start=start,end=end)
assert_allclose(tool.extents, (0.0, 100.0, 10.0, 120.0))

# resize NE handle
Expand DownExpand Up@@ -305,16 +312,16 @@ def test_rectangle_resize_center(ax, add_state):
xdata_new, ydata_new = xdata + xdiff, ydata + ydiff
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
key=use_key)
asserttool.extents == (xdata_new, extents[1] - xdiff,
ydata_new, extents[3] - ydiff)
assert_allclose(tool.extents, (xdata_new, extents[1] - xdiff,
ydata_new, extents[3] - ydiff))


@pytest.mark.parametrize('add_state', [True, False])
def test_rectangle_resize_square(ax, add_state):
tool = widgets.RectangleSelector(ax, interactive=True)
# Create rectangle
click_and_drag(tool, start=(70, 65), end=(120, 115))
asserttool.extents ==(70.0, 120.0, 65.0, 115.0)
assert_allclose(tool.extents,(70.0, 120.0, 65.0, 115.0))

if add_state:
tool.add_state('square')
Expand All@@ -329,8 +336,8 @@ def test_rectangle_resize_square(ax, add_state):
xdata_new, ydata_new = xdata + xdiff, ydata + ydiff
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
key=use_key)
asserttool.extents == (extents[0], xdata_new,
extents[2], extents[3] + xdiff)
assert_allclose(tool.extents, (extents[0], xdata_new,
extents[2], extents[3] + xdiff))

# resize E handle
extents = tool.extents
Expand DownExpand Up@@ -379,11 +386,12 @@ def test_rectangle_resize_square(ax, add_state):
xdata_new, ydata_new = xdata + xdiff, ydata + ydiff
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new),
key=use_key)
assert_allclose(tool.extents, (extents[0] + ydiff, extents[1],
ydata_new, extents[3]))
assert_allclose(tool.extents, (xdata_new, extents[1],
extents[2] + xdiff, extents[3]))


def test_rectangle_resize_square_center(ax):
ax.set_aspect(1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

What's the reason for having to add this? (here and in some other tests too)

tool = widgets.RectangleSelector(ax, interactive=True)
# Create rectangle
click_and_drag(tool, start=(70, 65), end=(120, 115))
Expand DownExpand Up@@ -449,6 +457,7 @@ def test_rectangle_resize_square_center(ax):
@pytest.mark.parametrize('selector_class',
[widgets.RectangleSelector, widgets.EllipseSelector])
def test_rectangle_rotate(ax, selector_class):
ax.set_aspect(1)
tool = selector_class(ax, interactive=True)
# Draw rectangle
click_and_drag(tool, start=(100, 100), end=(130, 140))
Expand All@@ -462,19 +471,19 @@ def test_rectangle_rotate(ax, selector_class):
click_and_drag(tool, start=(130, 140), end=(120, 145))
do_event(tool, 'on_key_press', key='r')
assert len(tool._state) == 0
# Extentsshouldn'tchange(asshape of rectangle hasn't changed)
asserttool.extents == (100, 130, 100, 140)
# Extents change asthe selector remains rigid in display coordinates
assert_allclose(tool.extents, (110.10, 119.90, 95.49, 144.51), atol=0.01)
assert_allclose(tool.rotation, 25.56, atol=0.01)
tool.rotation = 45
assert tool.rotation == 45
# Corners should move
assert_allclose(tool.corners,
np.array([[118.53, 139.75, 111.46, 90.25],
[95.25, 116.46, 144.75, 123.54]]), atol=0.01)
np.array([[110.10, 131.31, 103.03, 81.81],
[95.49, 116.70, 144.98, 123.77]]), atol=0.01)

# Scale using top-right corner
click_and_drag(tool, start=(110, 145), end=(110, 160))
assert_allclose(tool.extents, (100, 139.75, 100, 151.82), atol=0.01)
assert_allclose(tool.extents, (110, 110, 145, 160), atol=0.01)

if selector_class == widgets.RectangleSelector:
with pytest.raises(ValueError):
Expand All@@ -496,36 +505,38 @@ def test_rectangle_add_remove_set(ax):

@pytest.mark.parametrize('use_data_coordinates', [False, True])
def test_rectangle_resize_square_center_aspect(ax, use_data_coordinates):
ax.set_aspect(0.8)
ax = get_ax()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Why the call toget_ax() here (instead of just usingax)?

ax.set_aspect(0.5)
# Need to call a draw to update ax.transData
plt.gcf().canvas.draw()

tool = widgets.RectangleSelector(ax, interactive=True,
use_data_coordinates=use_data_coordinates)
# Create rectangle
click_and_drag(tool, start=(70, 65), end=(120, 115))
assert_allclose(tool.extents, (70.0, 120.0, 65.0, 115.0))
tool.add_state('square')
tool.add_state('center')
# Create rectangle, width 50 in data coordinates
click_and_drag(tool, start=(70, 65), end=(120, 75))

if use_data_coordinates:
# resize E handle
extents = tool.extents
xdata, ydata, width = extents[1], extents[3], extents[1] - extents[0]
xdiff, ycenter = 10, extents[2] + (extents[3] - extents[2]) / 2
xdata_new, ydata_new = xdata + xdiff, ydata
ychange = width / 2 + xdiff
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new))
assert_allclose(tool.extents, [extents[0] - xdiff, xdata_new,
ycenter - ychange, ycenter + ychange])
assert_allclose(tool.extents, (20, 120, 15, 115))
else:
# resize E handle
extents = tool.extents
xdata, ydata = extents[1], extents[3]
xdiff = 10
xdata_new, ydata_new = xdata + xdiff, ydata
ychange = xdiff * 1 / tool._aspect_ratio_correction
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new))
assert_allclose(tool.extents, [extents[0] - xdiff, xdata_new,
46.25, 133.75])
assert_allclose(tool.extents, (20, 120, -35, 165))

# resize E handle
extents = tool.extents
xdata, ydata = extents[1], extents[3]
xdiff = 10
xdata_new, ydata_new = xdata + xdiff, ydata
if use_data_coordinates:
# In data coordinates the difference should be equal in both directions
ydiff = xdiff
else:
# In display coordinates, the change in data coordinates should be
# different in each direction
ydiff = xdiff / tool.ax._get_aspect_ratio()
click_and_drag(tool, start=(xdata, ydata), end=(xdata_new, ydata_new))
assert_allclose(tool.extents, [extents[0] - xdiff, xdata_new,
extents[2] - ydiff, extents[3] + ydiff])


def test_ellipse(ax):
Expand DownExpand Up@@ -1631,6 +1642,7 @@ def test_polygon_selector_verts_setter(fig_test, fig_ref, draw_bounding_box):
]
for (etype, event_args) in event_sequence:
do_event(tool_ref, etype, **event_args)
np.testing.assert_allclose(tool_ref.verts, verts)


def test_polygon_selector_box(ax):
Expand All@@ -1645,10 +1657,19 @@ def test_polygon_selector_box(ax):
*polygon_place_vertex(*verts[0]),
]

# Set smaller axes limits to reduce errors in converting from data to
# display coords. The canvas size is 640 x 640, so we need a tolerance of
# (data width / canvas width) = 50 / 640 ~ 0.08 when comparing points in
# data space
ax.set_xlim(-5, 45)
ax.set_ylim(-5, 45)
atol = 0.08

# Create selector
tool = widgets.PolygonSelector(ax, draw_bounding_box=True)
for (etype, event_args) in event_sequence:
do_event(tool, etype, **event_args)
np.testing.assert_allclose(tool.verts, verts, atol=atol)

# In order to trigger the correct callbacks, trigger events on the canvas
# instead of the individual tools
Expand All@@ -1663,7 +1684,7 @@ def test_polygon_selector_box(ax):
MouseEvent(
"button_release_event", canvas, *t.transform((20, 20)), 1)._process()
np.testing.assert_allclose(
tool.verts, [(10, 0), (0, 10), (10, 20), (20, 10)])
tool.verts, [(10, 0), (0, 10), (10, 20), (20, 10)], atol=atol)

# Move using the center of the bounding box
MouseEvent(
Expand All@@ -1673,20 +1694,20 @@ def test_polygon_selector_box(ax):
MouseEvent(
"button_release_event", canvas, *t.transform((30, 30)), 1)._process()
np.testing.assert_allclose(
tool.verts, [(30, 20), (20, 30), (30, 40), (40, 30)])
tool.verts, [(30, 20), (20, 30), (30, 40), (40, 30)], atol=atol)

# Remove a point from the polygon and check that the box extents update
np.testing.assert_allclose(
tool._box.extents, (20.0, 40.0, 20.0, 40.0))
tool._box.extents, (20.0, 40.0, 20.0, 40.0), atol=atol)

MouseEvent(
"button_press_event", canvas, *t.transform((30, 20)), 3)._process()
MouseEvent(
"button_release_event", canvas, *t.transform((30, 20)), 3)._process()
np.testing.assert_allclose(
tool.verts, [(20, 30), (30, 40), (40, 30)])
tool.verts, [(20, 30), (30, 40), (40, 30)], atol=atol)
np.testing.assert_allclose(
tool._box.extents, (20.0, 40.0, 30.0, 40.0))
tool._box.extents, (20.0, 40.0, 30.0, 40.0), atol=atol)


def test_polygon_selector_clear_method(ax):
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp