@@ -370,8 +370,9 @@ def __init__(self, ax, label, valmin, valmax, *, valinit=0.5, valfmt=None,
370
370
The slider initial position.
371
371
372
372
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.
375
376
376
377
closedmin : bool, default: True
377
378
Whether the slider interval is closed on the bottom.
@@ -553,7 +554,10 @@ def _update(self, event):
553
554
def _format (self , val ):
554
555
"""Pretty-print *val*."""
555
556
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
557
561
else :
558
562
_ , s , _ = self ._fmt .format_ticks ([self .valmin , val , self .valmax ])
559
563
# fmt.get_offset is actually the multiplicative factor, if any.
@@ -650,9 +654,11 @@ def __init__(
650
654
The initial positions of the slider. If None the initial positions
651
655
will be at the 25th and 75th percentiles of the range.
652
656
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.
656
662
657
663
closedmin : bool, default: True
658
664
Whether the slider interval is closed on the bottom.
@@ -896,7 +902,10 @@ def _update(self, event):
896
902
def _format (self , val ):
897
903
"""Pretty-print *val*."""
898
904
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 ]} )"
900
909
else :
901
910
_ , s1 , s2 , _ = self ._fmt .format_ticks (
902
911
[self .valmin , * val , self .valmax ]
0 commit comments