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