Skip to content

Commit 21b76ae

Browse files
authored
Merge pull request matplotlib#21881 from dstansby/verts-setter
2 parents 738c9a9 + dce6cc2 commit 21b76ae

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Setting PolygonSelector vertices
2+
--------------------------------
3+
The vertices of `~matplotlib.widgets.PolygonSelector` can now be set
4+
programmatically by using the `~matplotlib.widgets.PolygonSelector.verts`
5+
property. Setting the vertices this way will reset the selector, and create
6+
a new complete selector with the supplied vertices.

lib/matplotlib/tests/test_widgets.py

+22
Original file line numberDiff line numberDiff line change
@@ -1442,6 +1442,28 @@ def onselect(vertices):
14421442
assert tool.verts == verts[0:2]
14431443

14441444

1445+
@check_figures_equal()
1446+
def test_polygon_selector_verts_setter(fig_test, fig_ref):
1447+
verts = [(0.1, 0.4), (0.5, 0.9), (0.3, 0.2)]
1448+
ax_test = fig_test.add_subplot()
1449+
1450+
def onselect(vertices):
1451+
pass
1452+
1453+
tool_test = widgets.PolygonSelector(ax_test, onselect)
1454+
tool_test.verts = verts
1455+
assert tool_test.verts == verts
1456+
1457+
ax_ref = fig_ref.add_subplot()
1458+
tool_ref = widgets.PolygonSelector(ax_ref, onselect)
1459+
event_sequence = (polygon_place_vertex(*verts[0]) +
1460+
polygon_place_vertex(*verts[1]) +
1461+
polygon_place_vertex(*verts[2]) +
1462+
polygon_place_vertex(*verts[0]))
1463+
for (etype, event_args) in event_sequence:
1464+
do_event(tool_ref, etype, **event_args)
1465+
1466+
14451467
@pytest.mark.parametrize(
14461468
"horizOn, vertOn",
14471469
[(True, True), (True, False), (False, True)],

lib/matplotlib/widgets.py

+16
Original file line numberDiff line numberDiff line change
@@ -3786,6 +3786,22 @@ def verts(self):
37863786
"""The polygon vertices, as a list of ``(x, y)`` pairs."""
37873787
return list(zip(self._xs[:-1], self._ys[:-1]))
37883788

3789+
@verts.setter
3790+
def verts(self, xys):
3791+
"""
3792+
Set the polygon vertices.
3793+
3794+
This will remove any pre-existing vertices, creating a complete polygon
3795+
with the new vertices.
3796+
"""
3797+
self._xs = [xy[0] for xy in xys]
3798+
self._xs.append(self._xs[0])
3799+
self._ys = [xy[1] for xy in xys]
3800+
self._ys.append(self._ys[0])
3801+
self._selection_completed = True
3802+
self.set_visible(True)
3803+
self._draw_polygon()
3804+
37893805

37903806
class Lasso(AxesWidget):
37913807
"""

0 commit comments

Comments
 (0)