Skip to content

Commit 41ba77f

Browse files
committed
{,Range}Slider: accept callable valfmt arguments
1 parent bda958a commit 41ba77f

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

lib/matplotlib/widgets.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,10 @@ def _update(self, event):
547547
def _format(self, val):
548548
"""Pretty-print *val*."""
549549
if self.valfmt is not None:
550-
return self.valfmt % val
550+
if callable(self.valfmt):
551+
return self.valfmt(val)
552+
else:
553+
return self.valfmt % val
551554
else:
552555
_, s, _ = self._fmt.format_ticks([self.valmin, val, self.valmax])
553556
# fmt.get_offset is actually the multiplicative factor, if any.
@@ -890,7 +893,10 @@ def _update(self, event):
890893
def _format(self, val):
891894
"""Pretty-print *val*."""
892895
if self.valfmt is not None:
893-
return f"({self.valfmt % val[0]}, {self.valfmt % val[1]})"
896+
if callable(self.valfmt):
897+
self.valfmt(val)
898+
else:
899+
return f"({self.valfmt % val[0]}, {self.valfmt % val[1]})"
894900
else:
895901
_, s1, s2, _ = self._fmt.format_ticks(
896902
[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]
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],
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 | None | Callable[[Sequence[float]], str] = ...,
134134
closedmin: bool = ...,
135135
closedmax: bool = ...,
136136
dragging: bool = ...,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy