@@ -364,8 +364,9 @@ def __init__(self, ax, label, valmin, valmax, *, valinit=0.5, valfmt=None,
364
364
The slider initial position.
365
365
366
366
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.
369
370
370
371
closedmin : bool, default: True
371
372
Whether the slider interval is closed on the bottom.
@@ -547,7 +548,10 @@ def _update(self, event):
547
548
def _format (self , val ):
548
549
"""Pretty-print *val*."""
549
550
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
551
555
else :
552
556
_ , s , _ = self ._fmt .format_ticks ([self .valmin , val , self .valmax ])
553
557
# fmt.get_offset is actually the multiplicative factor, if any.
@@ -644,9 +648,11 @@ def __init__(
644
648
The initial positions of the slider. If None the initial positions
645
649
will be at the 25th and 75th percentiles of the range.
646
650
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.
650
656
651
657
closedmin : bool, default: True
652
658
Whether the slider interval is closed on the bottom.
@@ -890,7 +896,10 @@ def _update(self, event):
890
896
def _format (self , val ):
891
897
"""Pretty-print *val*."""
892
898
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 ]} )"
894
903
else :
895
904
_ , s1 , s2 , _ = self ._fmt .format_ticks (
896
905
[self .valmin , * val , self .valmax ]
0 commit comments