Skip to content

Commit 6368cc5

Browse files
kbturktimhoffm
andauthored
Added optional props argument to Lasso Widget __init__ to customize Lasso line (matplotlib#26594)
* Added the ability for Lasso color, style, and lw to be set in the init function * Added props option to Lasso widget updated Lasso documentation with props updated Lasso documentation with props * wrote a test for the lasso tool Lasso test passes * fixed whitespace issues * fixed whitespace on test, added some test code to make sure defaults worked as intended * fixed whitespace in widget.py * updated with suggestions from oscargus * corrected over indented docstring * ...fixed indentions * docstrings * doc-string corrections * Update lib/matplotlib/widgets.py --------- Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>
1 parent 03a7ff0 commit 6368cc5

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

lib/matplotlib/tests/test_widgets.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,27 @@ def test_lasso_selector_set_props(ax):
10201020
assert artist.get_alpha() == 0.3
10211021

10221022

1023+
def test_lasso_set_props(ax):
1024+
onselect = mock.Mock(spec=noop, return_value=None)
1025+
tool = widgets.Lasso(ax, (100, 100), onselect)
1026+
line = tool.line
1027+
assert mcolors.same_color(line.get_color(), 'black')
1028+
assert line.get_linestyle() == '-'
1029+
assert line.get_lw() == 2
1030+
tool = widgets.Lasso(ax, (100, 100), onselect, props=dict(
1031+
linestyle='-', color='darkblue', alpha=0.2, lw=1))
1032+
1033+
line = tool.line
1034+
assert mcolors.same_color(line.get_color(), 'darkblue')
1035+
assert line.get_alpha() == 0.2
1036+
assert line.get_lw() == 1
1037+
assert line.get_linestyle() == '-'
1038+
line.set_color('r')
1039+
line.set_alpha(0.3)
1040+
assert mcolors.same_color(line.get_color(), 'r')
1041+
assert line.get_alpha() == 0.3
1042+
1043+
10231044
def test_CheckButtons(ax):
10241045
labels = ('a', 'b', 'c')
10251046
check = widgets.CheckButtons(ax, labels, (True, False, True))

lib/matplotlib/widgets.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4142,18 +4142,29 @@ class Lasso(AxesWidget):
41424142
Whether to use blitting for faster drawing (if supported by the
41434143
backend). See the tutorial :ref:`blitting`
41444144
for details.
4145-
"""
4145+
props: dict, optional
4146+
Lasso line properties. See `.Line2D` for valid properties.
4147+
Default *props* are::
4148+
4149+
{'linestyle' : '-', 'color' : 'black', 'lw' : 2}
41464150
4147-
def __init__(self, ax, xy, callback, *, useblit=True):
4151+
.. versionadded:: 3.9
4152+
"""
4153+
def __init__(self, ax, xy, callback, *, useblit=True, props=None):
41484154
super().__init__(ax)
41494155

41504156
self.useblit = useblit and self.canvas.supports_blit
41514157
if self.useblit:
41524158
self.background = self.canvas.copy_from_bbox(self.ax.bbox)
41534159

4160+
style = {'linestyle': '-', 'color': 'black', 'lw': 2}
4161+
4162+
if props is not None:
4163+
style.update(props)
4164+
41544165
x, y = xy
41554166
self.verts = [(x, y)]
4156-
self.line = Line2D([x], [y], linestyle='-', color='black', lw=2)
4167+
self.line = Line2D([x], [y], **style)
41574168
self.ax.add_line(self.line)
41584169
self.callback = callback
41594170
self.connect_event('button_release_event', self.onrelease)

lib/matplotlib/widgets.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ class Lasso(AxesWidget):
478478
callback: Callable[[list[tuple[float, float]]], Any],
479479
*,
480480
useblit: bool = ...,
481+
props: dict[str, Any] | None = ...,
481482
) -> None: ...
482483
def onrelease(self, event: Event) -> None: ...
483484
def onmove(self, event: Event) -> None: ...

0 commit comments

Comments
 (0)