Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 40a47e5

Browse files
authored
Merge pull request #30362 from doronbehar/callableSlider_format
{,Range}Slider: accept callable valfmt arguments
2 parents ed12550 + b6d7441 commit 40a47e5

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Callable *valfmt* for ``Slider`` and ``RangeSlider``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
In addition to the existing %-format string, the *valfmt* parameter of
5+
`~.matplotlib.widgets.Slider` and `~.matplotlib.widgets.RangeSlider` now
6+
also accepts a callable of the form ``valfmt(val: float) -> str``.

lib/matplotlib/widgets.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,9 @@ def __init__(self, ax, label, valmin, valmax, *, valinit=0.5, valfmt=None,
370370
The slider initial position.
371371
372372
valfmt : str, default: None
373-
%-format string used to format the slider value. If None, a
374-
`.ScalarFormatter` is used instead.
373+
The way to format the slider value. If a string, it must be in %-format.
374+
If a callable, it must have the signature ``valfmt(val: float) -> str``.
375+
If None, a `.ScalarFormatter` is used.
375376
376377
closedmin : bool, default: True
377378
Whether the slider interval is closed on the bottom.
@@ -553,7 +554,10 @@ def _update(self, event):
553554
def _format(self, val):
554555
"""Pretty-print *val*."""
555556
if self.valfmt is not None:
556-
return self.valfmt % val
557+
if callable(self.valfmt):
558+
return self.valfmt(val)
559+
else:
560+
return self.valfmt % val
557561
else:
558562
_, s, _ = self._fmt.format_ticks([self.valmin, val, self.valmax])
559563
# fmt.get_offset is actually the multiplicative factor, if any.
@@ -650,9 +654,11 @@ def __init__(
650654
The initial positions of the slider. If None the initial positions
651655
will be at the 25th and 75th percentiles of the range.
652656
653-
valfmt : str, default: None
654-
%-format string used to format the slider values. If None, a
655-
`.ScalarFormatter` is used instead.
657+
valfmt : str or callable, default: None
658+
The way to format the range's minimal and maximal values. If a
659+
string, it must be in %-format. If a callable, it must have the
660+
signature ``valfmt(val: float) -> str``. If None, a
661+
`.ScalarFormatter` is used.
656662
657663
closedmin : bool, default: True
658664
Whether the slider interval is closed on the bottom.
@@ -896,7 +902,10 @@ def _update(self, event):
896902
def _format(self, val):
897903
"""Pretty-print *val*."""
898904
if self.valfmt is not None:
899-
return f"({self.valfmt % val[0]}, {self.valfmt % val[1]})"
905+
if callable(self.valfmt):
906+
return f"({self.valfmt(val[0])}, {self.valfmt(val[1])})"
907+
else:
908+
return f"({self.valfmt % val[0]}, {self.valfmt % val[1]})"
900909
else:
901910
_, s1, s2, _ = self._fmt.format_ticks(
902911
[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
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,
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)