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

Commit28fac64

Browse files
committed
Move polygon only when the pointer is on the polygon
1 parent55cf8c7 commit28fac64

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

‎lib/matplotlib/tests/test_widgets.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,10 +1469,10 @@ def test_polygon_selector(draw_bounding_box):
14691469
*polygon_place_vertex(50,150),
14701470
*polygon_place_vertex(50,50),
14711471
('on_key_press',dict(key='shift')),
1472+
('onmove',dict(xdata=75,ydata=75)),
1473+
('press',dict(xdata=75,ydata=75)),
14721474
('onmove',dict(xdata=100,ydata=100)),
1473-
('press',dict(xdata=100,ydata=100)),
1474-
('onmove',dict(xdata=125,ydata=125)),
1475-
('release',dict(xdata=125,ydata=125)),
1475+
('release',dict(xdata=100,ydata=100)),
14761476
('on_key_release',dict(key='shift')),
14771477
]
14781478
check_selector(event_sequence,expected_result,2)
@@ -1719,6 +1719,31 @@ def test_polygon_selector_clear_method(ax):
17191719
np.testing.assert_equal(artist.get_xydata(), [(0,0)])
17201720

17211721

1722+
deftest_polygon_selector_move(ax):
1723+
onselect=mock.Mock(spec=noop,return_value=None)
1724+
tool=widgets.PolygonSelector(ax,onselect)
1725+
vertices= [(10,40), (50,90), (30,20)]
1726+
tool.verts=vertices
1727+
1728+
# don't move polygon when pointer is outside of the polygon
1729+
press_data= (100,100)
1730+
release_data= (110,110)
1731+
click_and_drag(tool,start=press_data,end=release_data,key="shift")
1732+
asserttool.verts==vertices
1733+
1734+
# don't move polygon when shift key is not press
1735+
press_data= (25,45)
1736+
release_data= (35,55)
1737+
click_and_drag(tool,start=press_data,end=release_data,key=None)
1738+
asserttool.verts==vertices
1739+
1740+
# move polygon when the pointer is on polygon
1741+
press_data= (25,45)
1742+
release_data= (35,55)
1743+
click_and_drag(tool,start=press_data,end=release_data,key="shift")
1744+
np.testing.assert_allclose(tool.verts,np.array(vertices)+10)
1745+
1746+
17221747
@pytest.mark.parametrize("horizOn", [False,True])
17231748
@pytest.mark.parametrize("vertOn", [False,True])
17241749
deftest_MultiCursor(horizOn,vertOn):

‎lib/matplotlib/widgets.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4018,7 +4018,12 @@ def _onmove(self, event):
40184018
self._xys[-1]=self._get_data_coords(event)
40194019

40204020
# Move all vertices.
4021-
elif'move_all'inself._stateandself._eventpress:
4021+
elif (
4022+
'move_all'inself._stateandself._eventpressand
4023+
# Allow `move_all` when not completed
4024+
# or if the event is contained in the polygon
4025+
(notself._selection_completedorself._contains(event))
4026+
):
40224027
xdata,ydata=self._get_data_coords(event)
40234028
dx=xdata-self._eventpress.xdata
40244029
dy=ydata-self._eventpress.ydata
@@ -4093,6 +4098,11 @@ def _draw_polygon(self):
40934098
self._draw_polygon_without_update()
40944099
self.update()
40954100

4101+
def_contains(self,event):
4102+
returnmpl.path.Path(self.verts).contains_points(
4103+
[self._get_data_coords(event), ]
4104+
)
4105+
40964106
@property
40974107
defverts(self):
40984108
"""The polygon vertices, as a list of ``(x, y)`` pairs."""

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp