Skip to content

Fix setting artists properties of selectors #20693

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
4 changes: 4 additions & 0 deletions doc/api/next_api_changes/deprecations/20693-EP.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
PolygonSelector
^^^^^^^^^^^^^^^
The *line* attribute is deprecated. If you want to change the selector
artist properties, use the ``set_props`` or ``set_handle_props`` methods.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Setting artist properties of selectors
--------------------------------------

The artist properties of selectors can be changed using the ``set_props`` and
``set_handle_props`` methods.
97 changes: 95 additions & 2 deletions lib/matplotlib/tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,34 @@ def onselect(epress, erelease):
assert tool.center == (180, 190)


def test_rectangle_selector_set_props_handle_props():
ax = get_ax()

def onselect(epress, erelease):
pass

tool = widgets.RectangleSelector(ax, onselect, interactive=True,
props=dict(facecolor='b', alpha=0.2),
handle_props=dict(alpha=0.5))
# Create rectangle
do_event(tool, 'press', xdata=0, ydata=10, button=1)
do_event(tool, 'onmove', xdata=100, ydata=120, button=1)
do_event(tool, 'release', xdata=100, ydata=120, button=1)

artist = tool._selection_artist
assert artist.get_facecolor() == mcolors.to_rgba('b', alpha=0.2)
tool.set_props(facecolor='r', alpha=0.3)
assert artist.get_facecolor() == mcolors.to_rgba('r', alpha=0.3)

for artist in tool._handles_artists:
assert artist.get_markeredgecolor() == 'black'
assert artist.get_alpha() == 0.5
tool.set_handle_props(markeredgecolor='r', alpha=0.3)
for artist in tool._handles_artists:
assert artist.get_markeredgecolor() == 'r'
assert artist.get_alpha() == 0.3


def test_ellipse():
"""For ellipse, test out the key modifiers"""
ax = get_ax()
Expand Down Expand Up @@ -185,9 +213,9 @@ def onselect(epress, erelease):

# Check that marker_props worked.
assert mcolors.same_color(
tool._corner_handles.artist.get_markerfacecolor(), 'r')
tool._corner_handles.artists[0].get_markerfacecolor(), 'r')
assert mcolors.same_color(
tool._corner_handles.artist.get_markeredgecolor(), 'b')
tool._corner_handles.artists[0].get_markeredgecolor(), 'b')


@pytest.mark.parametrize('interactive', [True, False])
Expand Down Expand Up @@ -404,6 +432,34 @@ def onselect(*args):
tool.direction = 'invalid_string'


def test_span_selector_set_props_handle_props():
ax = get_ax()

def onselect(epress, erelease):
pass

tool = widgets.SpanSelector(ax, onselect, 'horizontal', interactive=True,
props=dict(facecolor='b', alpha=0.2),
handle_props=dict(alpha=0.5))
# Create rectangle
do_event(tool, 'press', xdata=0, ydata=10, button=1)
do_event(tool, 'onmove', xdata=100, ydata=120, button=1)
do_event(tool, 'release', xdata=100, ydata=120, button=1)

artist = tool._selection_artist
assert artist.get_facecolor() == mcolors.to_rgba('b', alpha=0.2)
tool.set_props(facecolor='r', alpha=0.3)
assert artist.get_facecolor() == mcolors.to_rgba('r', alpha=0.3)

for artist in tool._handles_artists:
assert artist.get_color() == 'b'
assert artist.get_alpha() == 0.5
tool.set_handle_props(color='r', alpha=0.3)
for artist in tool._handles_artists:
assert artist.get_color() == 'r'
assert artist.get_alpha() == 0.3


def test_tool_line_handle():
ax = get_ax()

Expand Down Expand Up @@ -781,6 +837,43 @@ def test_polygon_selector():
check_polygon_selector(event_sequence, expected_result, 1)


def test_polygon_selector_set_props_handle_props():
ax = get_ax()

ax._selections_count = 0

def onselect(vertices):
ax._selections_count += 1
ax._current_result = vertices

tool = widgets.PolygonSelector(ax, onselect,
props=dict(color='b', alpha=0.2),
handle_props=dict(alpha=0.5))

event_sequence = (polygon_place_vertex(50, 50)
+ polygon_place_vertex(150, 50)
+ polygon_place_vertex(50, 150)
+ polygon_place_vertex(50, 50))

for (etype, event_args) in event_sequence:
do_event(tool, etype, **event_args)

artist = tool._selection_artist
assert artist.get_color() == 'b'
assert artist.get_alpha() == 0.2
tool.set_props(color='r', alpha=0.3)
assert artist.get_color() == 'r'
assert artist.get_alpha() == 0.3

for artist in tool._handles_artists:
assert artist.get_color() == 'b'
assert artist.get_alpha() == 0.5
tool.set_handle_props(color='r', alpha=0.3)
for artist in tool._handles_artists:
assert artist.get_color() == 'r'
assert artist.get_alpha() == 0.3


@pytest.mark.parametrize(
"horizOn, vertOn",
[(True, True), (True, False), (False, True)],
Expand Down
Loading