Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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 1 commit into from
Apr 9, 2020

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Apr 8, 2020

Using a ScalarFormatter works both for very small and for very large
values. We try to keep a user-configured ScalarFormatter if there's
already one (not that this was likely at all given that the formatter
is otherwise discarded by the call to set_x/yticks([])).

See https://discourse.matplotlib.org/t/matplotlib-slider-labels-stop-responding-with-very-small-numbers/21064/1 for an example of user confused by the default format string.

No tests, but one can try with e.g.

from matplotlib.widgets import Slider
from pylab import *

ax = plt.axes([.1, .5, .7, .1])
sl1 = Slider(ax, "foo", 2e8, 3e8)
ax = plt.axes([.1, .4, .7, .1])
sl2 = Slider(ax, "bar", 2e-8, 3e-8)
plt.show()

PR Summary

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

Using a ScalarFormatter works both for very small and for very large
values.  We try to keep a user-configured ScalarFormatter if there's
already one (not that this was likely at all given that the formatter
is otherwise discarded by the call to `set_x/yticks([])`).
@anntzer anntzer added this to the v3.3.0 milestone Apr 8, 2020
axis = ax.xaxis

self.valfmt = valfmt
self._fmt = axis.get_major_formatter()
Copy link
Member

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

Suggested change
self._fmt = axis.get_major_formatter()
self._formatter = axis.get_major_formatter()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fmt for format(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).

Copy link
Member

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 to format in my mind, but I do not expand to formatter. OTOH, this isn't really a big deal here.

@ImportanceOfBeingErnest
Copy link
Member

valfmt could also take any instance of matplotlib.ticker.Formatter as input?

@anntzer
Copy link
Contributor Author

anntzer commented Apr 9, 2020

You get to write that API change (edit: API extension) :-)
Note that it's not "just" using ScalarFormatter though -- special care needs to be taken into account so that the offset text (which is the multiplicative factor) doesn't get lost.
Rather, "any callable" may be better.

@jklymak
Copy link
Member

jklymak commented Apr 9, 2020

Lets go ahead and merge as an improvement, but maybe @ImportanceOfBeingErnest suggestion is a good one for a follow up?

@jklymak jklymak merged commit b731c29 into matplotlib:master Apr 9, 2020
@anntzer anntzer deleted the sliderfmt branch April 9, 2020 15:36
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()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will result in 3e-8 to be shown as 31e-8.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, I had axes.formatter.usemathtext in my rcdefaults, we just need to force that here...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants