Skip to content

Commit 205e907

Browse files
authored
Merge pull request matplotlib#25404 from QuLogic/selector-props
Move _SelectorWidget._props into SpanSelector
2 parents f21c545 + e138400 commit 205e907

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

lib/matplotlib/tests/test_widgets.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,19 @@ def test_lasso_selector(ax, kwargs):
987987
onselect.assert_called_once_with([(100, 100), (125, 125), (150, 150)])
988988

989989

990+
def test_lasso_selector_set_props(ax):
991+
onselect = mock.Mock(spec=noop, return_value=None)
992+
993+
tool = widgets.LassoSelector(ax, onselect, props=dict(color='b', alpha=0.2))
994+
995+
artist = tool._selection_artist
996+
assert mcolors.same_color(artist.get_color(), 'b')
997+
assert artist.get_alpha() == 0.2
998+
tool.set_props(color='r', alpha=0.3)
999+
assert mcolors.same_color(artist.get_color(), 'r')
1000+
assert artist.get_alpha() == 0.3
1001+
1002+
9901003
def test_CheckButtons(ax):
9911004
check = widgets.CheckButtons(ax, ('a', 'b', 'c'), (True, False, True))
9921005
assert check.get_status() == [True, False, True]

lib/matplotlib/widgets.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2457,15 +2457,16 @@ def artists(self):
24572457

24582458
def set_props(self, **props):
24592459
"""
2460-
Set the properties of the selector artist. See the `props` argument
2461-
in the selector docstring to know which properties are supported.
2460+
Set the properties of the selector artist.
2461+
2462+
See the *props* argument in the selector docstring to know which properties are
2463+
supported.
24622464
"""
24632465
artist = self._selection_artist
24642466
props = cbook.normalize_kwargs(props, artist)
24652467
artist.set(**props)
24662468
if self.useblit:
24672469
self.update()
2468-
self._props.update(props)
24692470

24702471
def set_handle_props(self, **handle_props):
24712472
"""
@@ -2658,7 +2659,6 @@ def __init__(self, ax, onselect, direction, minspan=0, useblit=False,
26582659
# but we maintain it until it is removed
26592660
self._pressv = None
26602661

2661-
self._props = props
26622662
self.onmove_callback = onmove_callback
26632663
self.minspan = minspan
26642664

@@ -2670,7 +2670,7 @@ def __init__(self, ax, onselect, direction, minspan=0, useblit=False,
26702670

26712671
# Reset canvas so that `new_axes` connects events.
26722672
self.canvas = None
2673-
self.new_axes(ax)
2673+
self.new_axes(ax, _props=props)
26742674

26752675
# Setup handles
26762676
self._handle_props = {
@@ -2686,7 +2686,7 @@ def __init__(self, ax, onselect, direction, minspan=0, useblit=False,
26862686
# prev attribute is deprecated but we still need to maintain it
26872687
self._prev = (0, 0)
26882688

2689-
def new_axes(self, ax):
2689+
def new_axes(self, ax, *, _props=None):
26902690
"""Set SpanSelector to operate on a new Axes."""
26912691
self.ax = ax
26922692
if self.canvas is not ax.figure.canvas:
@@ -2705,10 +2705,11 @@ def new_axes(self, ax):
27052705
else:
27062706
trans = ax.get_yaxis_transform()
27072707
w, h = 1, 0
2708-
rect_artist = Rectangle((0, 0), w, h,
2709-
transform=trans,
2710-
visible=False,
2711-
**self._props)
2708+
rect_artist = Rectangle((0, 0), w, h, transform=trans, visible=False)
2709+
if _props is not None:
2710+
rect_artist.update(_props)
2711+
elif self._selection_artist is not None:
2712+
rect_artist.update_from(self._selection_artist)
27122713

27132714
self.ax.add_patch(rect_artist)
27142715
self._selection_artist = rect_artist
@@ -3287,9 +3288,9 @@ def __init__(self, ax, onselect, *, minspanx=0, minspany=0, useblit=False,
32873288
if props is None:
32883289
props = dict(facecolor='red', edgecolor='black',
32893290
alpha=0.2, fill=True)
3290-
self._props = {**props, 'animated': self.useblit}
3291-
self._visible = self._props.pop('visible', self._visible)
3292-
to_draw = self._init_shape(**self._props)
3291+
props = {**props, 'animated': self.useblit}
3292+
self._visible = props.pop('visible', self._visible)
3293+
to_draw = self._init_shape(**props)
32933294
self.ax.add_patch(to_draw)
32943295

32953296
self._selection_artist = to_draw
@@ -3305,8 +3306,7 @@ def __init__(self, ax, onselect, *, minspanx=0, minspany=0, useblit=False,
33053306

33063307
if self._interactive:
33073308
self._handle_props = {
3308-
'markeredgecolor': (self._props or {}).get(
3309-
'edgecolor', 'black'),
3309+
'markeredgecolor': (props or {}).get('edgecolor', 'black'),
33103310
**cbook.normalize_kwargs(handle_props, Line2D)}
33113311

33123312
self._corner_order = ['SW', 'SE', 'NE', 'NW']
@@ -3942,13 +3942,13 @@ def __init__(self, ax, onselect, useblit=False,
39423942

39433943
if props is None:
39443944
props = dict(color='k', linestyle='-', linewidth=2, alpha=0.5)
3945-
self._props = {**props, 'animated': self.useblit}
3946-
self._selection_artist = line = Line2D([], [], **self._props)
3945+
props = {**props, 'animated': self.useblit}
3946+
self._selection_artist = line = Line2D([], [], **props)
39473947
self.ax.add_line(line)
39483948

39493949
if handle_props is None:
39503950
handle_props = dict(markeredgecolor='k',
3951-
markerfacecolor=self._props.get('color', 'k'))
3951+
markerfacecolor=props.get('color', 'k'))
39523952
self._handle_props = handle_props
39533953
self._polygon_handles = ToolHandles(self.ax, [], [],
39543954
useblit=self.useblit,

0 commit comments

Comments
 (0)