Skip to content

Corrected the slider handle styling #27674

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions lib/matplotlib/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,17 +417,18 @@ def __init__(self, ax, label, valmin, valmax, *, valinit=0.5, valfmt=None,
handle_style : dict
Properties of the slider handle. Default values are

========= ===== ======= ========================================
Key Value Default Description
========= ===== ======= ========================================
facecolor color 'white' The facecolor of the slider handle.
edgecolor color '.75' The edgecolor of the slider handle.
size int 10 The size of the slider handle in points.
========= ===== ======= ========================================

Other values will be transformed as marker{foo} and passed to the
`~.Line2D` constructor. e.g. ``handle_style = {'style'='x'}`` will
result in ``markerstyle = 'x'``.
=============== ======= ========================================
Key Default Description
=============== ======= ========================================
marker 'o' The style of the slider handle.
facecolor 'white' The facecolor of the slider handle.
edgecolor '.75' The edgecolor of the slider handle.
size 10 The size of the slider handle in points.
=============== ======= ========================================

Please refer to:
:doc:`gallery/lines_bars_and_markers/marker_reference`
for more information on styling.

Notes
-----
Expand All @@ -453,10 +454,12 @@ def __init__(self, ax, label, valmin, valmax, *, valinit=0.5, valfmt=None,
self.val = valinit
self.valinit = valinit

defaults = {'facecolor': 'white', 'edgecolor': '.75', 'size': 10}
defaults = {'marker': 'o', 'facecolor': 'white',
'edgecolor': '.75', 'size': 10}
handle_style = {} if handle_style is None else handle_style
marker_props = {
f'marker{k}': v for k, v in {**defaults, **handle_style}.items()
f'{"marker" * (not k.startswith("marker")) + k}': v \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your suggestion would broaden the API so that we'd accept handle_style={'markerfacecolor': ...}. It would be a separate debate whether that is reasonable, and it would also create some ambiguity handle_style={'markerfacecolor': 'r', 'facecolor': 'g'}. So for now, I suggest to only expand to accept "marker":

Suggested change
f'{"marker" * (not k.startswith("marker")) + k}': v \
f'{"marker" * (k != marker) + k}': v

Also this arithmethic is quite clever and we might want to be more explcit

            def prefix_marker(key):
                return key if key == "marker" else "marker" + key
                
            makrer_props = {
                prefix_marker(k): v for ...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your comments, they are very insightful and so to the point. I absolutely agree with everything you said :)
Would you mind pointing out to where this 'prefix_marker' function should be defined?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be a nested function unless we have some other place to use it, and it'd be more for self-documentation.

for k, v in {**defaults, **handle_style}.items()
}

if orientation == 'vertical':
Expand Down Expand Up @@ -485,7 +488,6 @@ def __init__(self, ax, label, valmin, valmax, *, valinit=0.5, valfmt=None,
handleXY = [[valinit], [0.5]]
self._handle, = ax.plot(
*handleXY,
"o",
**marker_props,
clip_on=False
)
Expand Down