-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Improve default formatter for Slider values. #17077
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
import numpy as np | ||
|
||
import matplotlib as mpl | ||
from . import cbook | ||
from . import cbook, ticker | ||
from .lines import Line2D | ||
from .patches import Circle, Rectangle, Ellipse | ||
from .transforms import blended_transform_factory | ||
|
@@ -256,7 +256,8 @@ class Slider(AxesWidget): | |
val : float | ||
Slider value. | ||
""" | ||
def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt='%1.2f', | ||
|
||
def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt=None, | ||
closedmin=True, closedmax=True, slidermin=None, | ||
slidermax=None, dragging=True, valstep=None, | ||
orientation='horizontal', **kwargs): | ||
|
@@ -278,8 +279,9 @@ def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt='%1.2f', | |
valinit : float, default: 0.5 | ||
The slider initial position. | ||
|
||
valfmt : str, default: "%1.2f" | ||
Used to format the slider value, fprint format string. | ||
valfmt : str, default: None | ||
%-format string used to format the slider value. If None, a | ||
`.ScalarFormatter` is used instead. | ||
|
||
closedmin : bool, default: True | ||
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', | |
self.poly = ax.axvspan(valmin, valinit, 0, 1, **kwargs) | ||
self.vline = ax.axvline(valinit, 0, 1, color='r', lw=1) | ||
|
||
self.valfmt = valfmt | ||
ax.set_yticks([]) | ||
if orientation == 'vertical': | ||
ax.set_ylim((valmin, valmax)) | ||
axis = ax.yaxis | ||
else: | ||
ax.set_xlim((valmin, valmax)) | ||
axis = ax.xaxis | ||
|
||
self.valfmt = valfmt | ||
self._fmt = axis.get_major_formatter() | ||
if not isinstance(self._fmt, ticker.ScalarFormatter): | ||
self._fmt = ticker.ScalarFormatter() | ||
self._fmt.set_axis(axis) | ||
self._fmt.set_useOffset(False) # No additive offset. | ||
|
||
ax.set_xticks([]) | ||
ax.set_yticks([]) | ||
ax.set_navigate(False) | ||
|
||
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', | |
verticalalignment='bottom', | ||
horizontalalignment='center') | ||
|
||
self.valtext = ax.text(0.5, -0.02, valfmt % valinit, | ||
self.valtext = ax.text(0.5, -0.02, self._format(valinit), | ||
transform=ax.transAxes, | ||
verticalalignment='top', | ||
horizontalalignment='center') | ||
|
@@ -374,7 +385,7 @@ def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt='%1.2f', | |
verticalalignment='center', | ||
horizontalalignment='right') | ||
|
||
self.valtext = ax.text(1.02, 0.5, valfmt % valinit, | ||
self.valtext = ax.text(1.02, 0.5, self._format(valinit), | ||
transform=ax.transAxes, | ||
verticalalignment='center', | ||
horizontalalignment='left') | ||
|
@@ -435,6 +446,15 @@ def _update(self, event): | |
if val not in [None, self.val]: | ||
self.set_val(val) | ||
|
||
def _format(self, val): | ||
"""Pretty-print *val*.""" | ||
if self.valfmt is not None: | ||
return self.valfmt % val | ||
else: | ||
_, s, _ = self._fmt.format_ticks([self.valmin, val, self.valmax]) | ||
# fmt.get_offset is actually the multiplicative factor, if any. | ||
return s + self._fmt.get_offset() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will result in 3e-8 to be shown as 31e-8. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops, I had |
||
|
||
def set_val(self, val): | ||
""" | ||
Set slider value to *val* | ||
|
@@ -451,7 +471,7 @@ def set_val(self, val): | |
xy[2] = val, 1 | ||
xy[3] = val, 0 | ||
self.poly.xy = xy | ||
self.valtext.set_text(self.valfmt % val) | ||
self.valtext.set_text(self._format(val)) | ||
if self.drawon: | ||
self.ax.figure.canvas.draw_idle() | ||
self.val = val | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this attribute is introduced here? If so, let's call it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fmt
forformat
(ter) is a pretty standard abbreviation, and this avoids an awkward linebreak a bit later which also helps from a legibility PoV. OTOH I don't really want to argue too much over this so if you are sure you prefer _formatter, let me know and I'll do the change (yes, it's a new attribute, so we can call it however we want).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I see
fmt
I expand toformat
in my mind, but I do not expand toformatter
. OTOH, this isn't really a big deal here.