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. | +---------------------------------+--------------------------------------------------+ 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 0a9f44a1b09e..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 two 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 + 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