8000 Merge pull request #17077 from anntzer/sliderfmt · matplotlib/matplotlib@b731c29 · GitHub
[go: up one dir, main page]

Skip to content

Commit b731c29

Browse files
authored
Merge pull request #17077 from anntzer/sliderfmt
Improve default formatter for Slider values.
2 parents dc15e0d + f9e7851 commit b731c29

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

doc/api/api_changes_3.3/behaviour.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,11 @@ support for it will be dropped in a future Matplotlib release.
155155
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
156156
Previously, keyword arguments were silently ignored when no positional
157157
arguments were given.
158+
159+
Default slider formatter
160+
~~~~~~~~~~~~~~~~~~~~~~~~
161+
The default method used to format `.Slider` values has been changed to use a
162+
`.ScalarFormatter` adapted the slider values limits. This should ensure that
163+
values are displayed with an appropriate number of significant digits even if
164+
they are much smaller or much bigger than 1. To restore the old behavior,
165+
explicitly pass a "%1.2f" as the *valfmt* parameter to `.Slider`.

lib/matplotlib/widgets.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import numpy as np
1717

1818
import matplotlib as mpl
19-
from . import cbook
19+
from . import cbook, ticker
2020
from .lines import Line2D
2121
from .patches import Circle, Rectangle, Ellipse
2222
from .transforms import blended_transform_factory
@@ -256,7 +256,8 @@ class Slider(AxesWidget):
256256
val : float
257257
Slider value.
258258
"""
259-
def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt='%1.2f',
259+
260+
def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt=None,
260261
closedmin=True, closedmax=True, slidermin=None,
261262
slidermax=None, dragging=True, valstep=None,
262263
orientation='horizontal', **kwargs):
@@ -278,8 +279,9 @@ def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt='%1.2f',
278279
valinit : float, default: 0.5
279280
The slider initial position.
280281
281-
valfmt : str, default: "%1.2f"
282-
Used to format the slider value, fprint format string.
282+
valfmt : str, default: None
283+
%-format string used to format the slider value. If None, a
284+
`.ScalarFormatter` is used instead.
283285
284286
closedmin : bool, default: True
285287
Whether the slider interval is closed on the bottom.
@@ -347,13 +349,22 @@ def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt='%1.2f',
347349
self.poly = ax.axvspan(valmin, valinit, 0, 1, **kwargs)
348350
self.vline = ax.axvline(valinit, 0, 1, color='r', lw=1)
349351

350-
self.valfmt = valfmt
351-
ax.set_yticks([])
352352
if orientation == 'vertical':
353353
ax.set_ylim((valmin, valmax))
354+
axis = ax.yaxis
354355
else:
355356
ax.set_xlim((valmin, valmax))
357+
axis = ax.xaxis
358+
359+
self.valfmt = valfmt
360+
self._fmt = axis.get_major_formatter()
361+
if not isinstance(self._fmt, ticker.ScalarFormatter):
362+
self._fmt = ticker.ScalarFormatter()
363+
self._fmt.set_axis(axis)
364+
self._fmt.set_useOffset(False) # No additive offset.
365+
356366
ax.set_xticks([])
367+
ax.set_yticks([])
357368
ax.set_navigate(False)
358369

359370
self.connect_event('button_press_event', self._update)
@@ -365,7 +376,7 @@ def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt='%1.2f',
365376
verticalalignment='bottom',
366377
horizontalalignment='center')
367378

368-
self.valtext = ax.text(0.5, -0.02, valfmt % valinit,
379+
self.valtext = ax.text(0.5, -0.02, self._format(valinit),
369380
transform=ax.transAxes,
370381
verticalalignment='top',
371382
horizontalalignment='center')
@@ -374,7 +385,7 @@ def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt='%1.2f',
374385
verticalalignment='center',
375386
horizontalalignment='right')
376387

377-
self.valtext = ax.text(1.02, 0.5, valfmt % valinit,
388+
self.valtext = ax.text(1.02, 0.5, self._format(valinit),
378389
transform=ax.transAxes,
379390
verticalalignment='center',
380391
horizontalalignment='left')
@@ -435,6 +446,15 @@ def _update(self, event):
435446
if val not in [None, self.val]:
436447
self.set_val(val)
437448

449+
def _format(self, val):
450+
"""Pretty-print *val*."""
451+
if self.valfmt is not None:
452+
return self.valfmt % val
453+
else:
454+
_, s, _ = self._fmt.format_ticks([self.valmin, val, self.valmax])
455+
# fmt.get_offset is actually the multiplicative factor, if any.
456+
return s + self._fmt.get_offset()
457+
438458
def set_val(self, val):
439459
"""
440460
Set slider value to *val*
@@ -451,7 +471,7 @@ def set_val(self, val):
451471
xy[2] = val, 1
452472
xy[3] = val, 0
453473
self.poly.xy = xy
454-
self.valtext.set_text(self.valfmt % val)
474+
self.valtext.set_text(self._format(val))
455475
if self.drawon:
456476
self.ax.figure.canvas.draw_idle()
457477
self.val = val

0 commit comments

Comments
 (0)
0