Skip to content

{,Range}Slider: accept callable valfmt arguments #30362

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
merged 3 commits into from
Aug 7, 2025
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions doc/users/next_whats_new/sliders_callable_valfmt.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Callable *valfmt* for ``Slider`` and ``RangeSlider``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In addition to the existing %-format string, the *valfmt* parameter of
`~.matplotlib.widgets.Slider` and `~.matplotlib.widgets.RangeSlider` now
also accepts a callable of the form ``valfmt(val: float) -> str``.
23 changes: 16 additions & 7 deletions lib/matplotlib/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,9 @@ def __init__(self, ax, label, valmin, valmax, *, valinit=0.5, valfmt=None,
The slider initial position.

valfmt : str, default: None
%-format string used to format the slider value. If None, a
`.ScalarFormatter` is used instead.
The way to format the slider value. If a string, it must be in %-format.
If a callable, it must have the signature ``valfmt(val: float) -> str``.
If None, a `.ScalarFormatter` is used.

closedmin : bool, default: True
Whether the slider interval is closed on the bottom.
Expand Down Expand Up @@ -547,7 +548,10 @@ def _update(self, event):
def _format(self, val):
"""Pretty-print *val*."""
if self.valfmt is not None:
return self.valfmt % val
if callable(self.valfmt):
return self.valfmt(val)
else:
return self.valfmt % val
else:
_, s, _ = self._fmt.format_ticks([self.valmin, val, self.valmax])
# fmt.get_offset is actually the multiplicative factor, if any.
Expand Down Expand Up @@ -644,9 +648,11 @@ def __init__(
The initial positions of the slider. If None the initial positions
will be at the 25th and 75th percentiles of the range.

valfmt : str, default: None
%-format string used to format the slider values. If None, a
`.ScalarFormatter` is used instead.
valfmt : str or callable, default: None
The way to format the range's minimal and maximal values. If a
string, it must be in %-format. If a callable, it must have the
signature ``valfmt(val: float) -> str``. If None, a
`.ScalarFormatter` is used.

closedmin : bool, default: True
Whether the slider interval is closed on the bottom.
Expand Down Expand Up @@ -890,7 +896,10 @@ def _update(self, event):
def _format(self, val):
"""Pretty-print *val*."""
if self.valfmt is not None:
return f"({self.valfmt % val[0]}, {self.valfmt % val[1]})"
if callable(self.valfmt):
return f"({self.valfmt(val[0])}, {self.valfmt(val[1])})"
else:
return f"({self.valfmt % val[0]}, {self.valfmt % val[1]})"
else:
_, s1, s2, _ = self._fmt.format_ticks(
[self.valmin, *val, self.valmax]
Expand Down
6 changes: 3 additions & 3 deletions lib/matplotlib/widgets.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class SliderBase(AxesWidget):
valmax: float
valstep: float | ArrayLike | None
drag_active: bool
valfmt: str
valfmt: str | Callable[[float], str] | None
def __init__(
self,
ax: Axes,
Expand All @@ -73,7 +73,7 @@ class SliderBase(AxesWidget):
closedmax: bool,
valmin: float,
valmax: float,
valfmt: str,
valfmt: str | Callable[[float], str] | None,
dragging: Slider | None,
valstep: float | ArrayLike | None,
) -> None: ...
Expand Down Expand Up @@ -130,7 +130,7 @@ class RangeSlider(SliderBase):
valmax: float,
*,
valinit: tuple[float, float] | None = ...,
valfmt: str | None = ...,
valfmt: str | Callable[[float], str] | None = ...,
closedmin: bool = ...,
closedmax: bool = ...,
dragging: bool = ...,
Expand Down
Loading