@@ -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 ]
0 commit comments