-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
Fix setting artists properties of selectors #20693
Conversation
@larsoner, does this API look good to you? |
Yep, and I tested it and it seems to work for us! |
Great, thanks @larsoner! |
d73f751
to
6a93943
Compare
This is ready for review |
I don't think this is a good idea. Having Should we keep them internal (and have public
That depends: How much control do we want to give to the user? |
190c96c
to
9e6394f
Compare
Agreed, I have added a
I would lean toward keeping it simple for now! |
lib/matplotlib/widgets.py
Outdated
self._direction = direction | ||
self.new_axes(self.ax) | ||
if self._interactive: | ||
self._setup_edge_handle(self._edge_handles._line_props) | ||
line = self._edge_handles.artists[0] | ||
line_props = dict(alpha=line.get_alpha(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to get the properties relevant to axvline
(the kwargs of axvline
)? line.properties()
returns too many properties.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not directly. https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.axvline.html#matplotlib.axes.Axes.axvline says
Valid keyword arguments are Line2D properties, with the exception of 'transform'.
That's because it's a Line2D
, but we enforce the vertical placement using a transform, so this is not user-settable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, indeed, but there are also other properties (internal properties?), which are not listed in https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.axvline.html#matplotlib.axes.Axes.axvline:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
line = ax.axvline(1.5)
props = line.properties()
del props['transform']
line.set(**props)
Will give the following error:
File "/home/eric/Dev/others/matplotlib/lib/matplotlib/artist.py", line 115, in <lambda>
cls.set = lambda self, **kwargs: Artist.set(self, **kwargs)
File "/home/eric/Dev/others/matplotlib/lib/matplotlib/artist.py", line 1155, in set
return self.update(kwargs)
File "/home/eric/Dev/others/matplotlib/lib/matplotlib/artist.py", line 1060, in update
raise AttributeError(f"{type(self).__name__!r} object "
AttributeError: 'Line2D' object has no property 'children'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Essentially looks good.
Since for now, we seem to need the update, set_props()
and set_handle_props()
are the right way to go. In that case, do people still need access to the properties artists
, selection_artist
, handle_artists
? If not, I'd start with keeping them private to keep the public API small. We can always make them public later if needed.
lib/matplotlib/widgets.py
Outdated
self._direction = direction | ||
self.new_axes(self.ax) | ||
if self._interactive: | ||
self._setup_edge_handle(self._edge_handles._line_props) | ||
line = self._edge_handles.artists[0] | ||
line_props = dict(alpha=line.get_alpha(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not directly. https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.axvline.html#matplotlib.axes.Axes.axvline says
Valid keyword arguments are Line2D properties, with the exception of 'transform'.
That's because it's a Line2D
, but we enforce the vertical placement using a transform, so this is not user-settable.
b0545dc
to
73ac533
Compare
This is ready for review, I have updated the description of the PR to reflect the current state. |
8f0e823
to
a871ca6
Compare
Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
382bc1d
to
cee023e
Compare
Thanks @QuLogic for the review, all comments should be sorted! |
Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>
Thanks @timhoffm, all done - CI had some hiccups and I restarted it. |
Thanks @ericpre! |
…693-on-v3.5.x Backport PR #20693 on branch v3.5.x (Fix setting artists properties of selectors)
PR Summary
Fix #20618 by adding
set_props
andset_handle_props
method.This PR needs a bit of tidying up but I would like to know if we are happy with this approach!A few others changes:
selector.artists
is now a property returning a tuple of all artists to disallow changing the artists through a public API_selection_artist
and_handles_artists
to access the corresponding artists internally.Remove the use ofSpanSelector._rect
and other, because it is already accessible throughartists[0]
, maybe this is still better to keep a separate attribute?Alternative could be:
props
andhandle_props
setterget_main_artist
get_handle_artists
, which could use asspan.get_main_artist().set(facecolor='green')
, forget_handle_artists
, it would be necessary to loop over the tuplePR Checklist
pytest
passes).flake8
on changed files to check).flake8-docstrings
and runflake8 --docstring-convention=all
).doc/users/next_whats_new/
(follow instructions in README.rst there).doc/api/next_api_changes/
(follow instructions in README.rst there).