Skip to content

Commit 4cdc046

Browse files
doronbehartimhoffm
andcommitted
{,Range}Slider: accept callable valfmt arguments
Co-authored-by: Tim Hoffmann <[email protected]>
1 parent 95cf154 commit 4cdc046

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

lib/matplotlib/widgets.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,9 @@ def __init__(self, ax, label, valmin, valmax, *, valinit=0.5, valfmt=None,
364364
The slider initial position.
365365
366366
valfmt : str, default: None
367-
%-format string used to format the slider value. If None, a
368-
`.ScalarFormatter` is used instead.
367+
The way to format the slider value. If a string, it must be in %-format.
368+
If a callable, it must have the signature ``valfmt(val: float) -> str``.
369+
If None, a `.ScalarFormatter` is used.
369370
370371
closedmin : bool, default: True
371372
Whether the slider interval is closed on the bottom.
@@ -547,7 +548,10 @@ def _update(self, event):
547548
def _format(self, val):
548549
"""Pretty-print *val*."""
549550
if self.valfmt is not None:
550-
return self.valfmt % val
551+
if callable(self.valfmt):
552+
return self.valfmt(val)
553+
else:
554+
return self.valfmt % val
551555
else:
552556
_, s, _ = self._fmt.format_ticks([self.valmin, val, self.valmax])
553557
# fmt.get_offset is actually the multiplicative factor, if any.
@@ -644,9 +648,11 @@ def __init__(
644648
The initial positions of the slider. If None the initial positions
645649
will be at the 25th and 75th percentiles of the range.
646650
647-
valfmt : str, default: None
648-
%-format string used to format the slider values. If None, a
649-
`.ScalarFormatter` is used instead.
651+
valfmt : str or callable, default: None
652+
The way to format the range's minimal and maximal values. If a
653+
string, it must be in %-format. If a callable, it must have the
654+
signature ``valfmt(val: float) -> str``. If None, a
655+
`.ScalarFormatter` is used.
650656
651657
closedmin : bool, default: True
652658
Whether the slider interval is closed on the bottom.
@@ -890,7 +896,10 @@ def _update(self, event):
890896
def _format(self, val):
891897
"""Pretty-print *val*."""
892898
if self.valfmt is not None:
893-
return f"({self.valfmt % val[0]}, {self.valfmt % val[1]})"
899+
if callable(self.valfmt):
900+
return f"({self.valfmt(val[0])}, {self.valfmt(val[1])})"
901+
else:
902+
return f"({self.valfmt % val[0]}, {self.valfmt % val[1]})"
894903
else:
895904
_, s1, s2, _ = self._fmt.format_ticks(
896905
[self.valmin, *val, self.valmax]

lib/matplotlib/widgets.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class SliderBase(AxesWidget):
6464
valmax: float
6565
valstep: float | ArrayLike | None
6666
drag_active: bool
67-
valfmt: str | None
67+
valfmt: str | Callable[[float], str] | None
6868
def __init__(
6969
self,
7070
ax: Axes,
@@ -73,7 +73,7 @@ class SliderBase(AxesWidget):
7373
closedmax: bool,
7474
valmin: float,
7575
valmax: float,
76-
valfmt: str | None,
76+
valfmt: str | Callable[[float], str] | None,
7777
dragging: Slider | None,
7878
valstep: float | ArrayLike | None,
7979
) -> None: ...
@@ -130,7 +130,7 @@ class RangeSlider(SliderBase):
130130
valmax: float,
131131
*,
132132
valinit: tuple[float, float] | None = ...,
133-
valfmt: str | None = ...,
133+
valfmt: str | Callable[[float], str] | None = ...,
134134
closedmin: bool = ...,
135135
closedmax: bool = ...,
136136
dragging: bool = ...,

0 commit comments

Comments
 (0)