From daa5ed67ae85bc03ce3f14ac80f89ccc0d0b4369 Mon Sep 17 00:00:00 2001 From: Eric Firing Date: Sun, 30 Oct 2016 15:33:21 -1000 Subject: [PATCH 1/3] STY: in ScalarFormatter, use offset to save at least 4 digits, not 2 Closes #7104. --- lib/matplotlib/ticker.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index 0a9f44a1b09e..4d7ca7297d64 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -689,9 +689,9 @@ def _compute_offset(self): # are no more than 1 apart at that precision? oom = 1 + next(oom for oom in itertools.count(oom_max, -1) if abs_max // 10 ** oom - abs_min // 10 ** oom > 1) - # Only use offset if it saves at least two significant digits. + # Only use offset if it saves at least 4 significant digits. self.offset = (sign * (abs_max // 10 ** oom) * 10 ** oom - if abs_max // 10 ** oom >= 10 + if abs_max // 10 ** oom >= 10**3 # 10**(4-1) else 0) def _set_orderOfMagnitude(self, range): From 255d4c31028bc1ecbb5267cb8c9aef3f54147b0a Mon Sep 17 00:00:00 2001 From: Eric Firing Date: Tue, 1 Nov 2016 21:50:24 -1000 Subject: [PATCH 2/3] add axes.formatter.offset_threshold rcParam: classic is 2, v2.0 is 4 --- lib/matplotlib/mpl-data/stylelib/classic.mplstyle | 4 ++++ lib/matplotlib/rcsetup.py | 1 + lib/matplotlib/tests/test_ticker.py | 2 +- lib/matplotlib/ticker.py | 6 ++++-- matplotlibrc.template | 7 +++++-- 5 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/mpl-data/stylelib/classic.mplstyle b/lib/matplotlib/mpl-data/stylelib/classic.mplstyle index 071ef5f2065c..f89e4ba5fee1 100644 --- a/lib/matplotlib/mpl-data/stylelib/classic.mplstyle +++ b/lib/matplotlib/mpl-data/stylelib/classic.mplstyle @@ -204,6 +204,10 @@ axes.formatter.useoffset : True # If True, the tick label formatter # to an offset when the data range is very # small compared to the minimum absolute # value of the data. +axes.formatter.offset_threshold : 2 # When useoffset is True, the offset + # will be used when it can remove + # at least this number of significant + # digits from tick labels. axes.unicode_minus : True # use unicode for the minus symbol # rather than hyphen. See diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index e2614b831433..b8dee2c13aa4 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -1085,6 +1085,7 @@ def validate_animation_writer_path(p): # Use the current locale to format ticks 'axes.formatter.use_mathtext': [False, validate_bool], 'axes.formatter.useoffset': [True, validate_bool], + 'axes.formatter.offset_threshold': [4, validate_int], 'axes.unicode_minus': [True, validate_bool], 'axes.color_cycle': [ ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', diff --git a/lib/matplotlib/tests/test_ticker.py b/lib/matplotlib/tests/test_ticker.py index 069c7c194a8d..fd6915885882 100644 --- a/lib/matplotlib/tests/test_ticker.py +++ b/lib/matplotlib/tests/test_ticker.py @@ -169,7 +169,7 @@ def test_SymmetricalLogLocator_set_params(): nose.tools.assert_equal(sym.numticks, 8) -@cleanup +@cleanup(style='classic') def test_ScalarFormatter_offset_value(): fig, ax = plt.subplots() formatter = ax.get_xaxis().get_major_formatter() diff --git a/lib/matplotlib/ticker.py b/lib/matplotlib/ticker.py index 4d7ca7297d64..439fb535c96f 100644 --- a/lib/matplotlib/ticker.py +++ b/lib/matplotlib/ticker.py @@ -505,6 +505,7 @@ def __init__(self, useOffset=None, useMathText=None, useLocale=None): if useOffset is None: useOffset = rcParams['axes.formatter.useoffset'] + self._offset_threshold = rcParams['axes.formatter.offset_threshold'] self.set_useOffset(useOffset) self._usetex = rcParams['text.usetex'] if useMathText is None: @@ -689,9 +690,10 @@ def _compute_offset(self): # are no more than 1 apart at that precision? oom = 1 + next(oom for oom in itertools.count(oom_max, -1) if abs_max // 10 ** oom - abs_min // 10 ** oom > 1) - # Only use offset if it saves at least 4 significant digits. + # Only use offset if it saves at least _offset_threshold digits. + n = self._offset_threshold - 1 self.offset = (sign * (abs_max // 10 ** oom) * 10 ** oom - if abs_max // 10 ** oom >= 10**3 # 10**(4-1) + if abs_max // 10 ** oom >= 10**n else 0) def _set_orderOfMagnitude(self, range): diff --git a/matplotlibrc.template b/matplotlibrc.template index 6d6549a1d868..b842b288772a 100644 --- a/matplotlibrc.template +++ b/matplotlibrc.template @@ -317,10 +317,13 @@ backend : $TEMPLATE_BACKEND # notation. #axes.formatter.useoffset : True # If True, the tick label formatter # will default to labeling ticks relative - # to an offset when the data range is very + # to an offset when the data range is # small compared to the minimum absolute # value of the data. - +#axes.formatter.offset_threshold : 4 # When useoffset is True, the offset + # will be used when it can remove + # at least this number of significant + # digits from tick labels. #axes.unicode_minus : True # use unicode for the minus symbol # rather than hyphen. See From 77dc47b8d1ffceae3303072e0164901702a1e41e Mon Sep 17 00:00:00 2001 From: Eric Firing Date: Wed, 2 Nov 2016 08:59:56 -1000 Subject: [PATCH 3/3] DOC: offset_threshold in whats_new and dflt_style_changes --- doc/users/dflt_style_changes.rst | 11 +++++++++++ doc/users/whats_new.rst | 9 ++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/doc/users/dflt_style_changes.rst b/doc/users/dflt_style_changes.rst index ee97f4401e1e..e0029a3531f8 100644 --- a/doc/users/dflt_style_changes.rst +++ b/doc/users/dflt_style_changes.rst @@ -1065,6 +1065,17 @@ Z-order +``ScalarFormatter`` tick label formatting with offsets +====================================================== + +With the default of ``rcParams['axes.formatter.useoffset'] = True``, +an offset will be used when it will save 4 or more digits. This can +be controlled with the new rcParam, ``axes.formatter.offset_threshold``. +To restore the previous behavior of using an offset to save 2 or more +digits, use ``rcParams['axes.formatter.offset_threshold'] = 2``. + + + ``AutoDateFormatter`` format strings ==================================== diff --git a/doc/users/whats_new.rst b/doc/users/whats_new.rst index 226f3ee1f572..a95fd72a078f 100644 --- a/doc/users/whats_new.rst +++ b/doc/users/whats_new.rst @@ -82,13 +82,16 @@ New rcparams added |`ytick.minor.right`, | | |`ytick.major.right` | | +---------------------------------+--------------------------------------------------+ -|`hist.bins` | the default number of bins to use in | +|`hist.bins` | The default number of bins to use in | | | `~matplotlib.axes.Axes.hist`. This can be an | | | `int`, a list of floats, or ``'auto'`` if numpy | | | >= 1.11 is installed. | +---------------------------------+--------------------------------------------------+ -|`lines.scale_dashes` | If the line dash patterns should scale with | -| | linewidth | +|`lines.scale_dashes` | Whether the line dash patterns should scale with | +| | linewidth. | ++---------------------------------+--------------------------------------------------+ +|`axes.formatter.offset_threshold`| Minimum number of digits saved in tick labels | +| | that triggers using an offset. | +---------------------------------+--------------------------------------------------+