8000 Merge pull request #7365 from efiring/offset_threshold_4 · matplotlib/matplotlib@6beddec · GitHub
[go: up one dir, main page]

Skip to content
10000

Commit 6beddec

Browse files
authored
Merge pull request #7365 from efiring/offset_threshold_4
API: in ScalarFormatter, use offset to save at least 4 digits, not 2
2 parents 4d692d8 + 77dc47b commit 6beddec

File tree

7 files changed

+32
-8
lines changed

7 files changed

+32
-8
lines changed

doc/users/dflt_style_changes.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,17 @@ Z-order
10681068

10691069

10701070

1071+
``ScalarFormatter`` tick label formatting with offsets
1072+
======================================================
1073+
1074+
With the default of ``rcParams['axes.formatter.useoffset'] = True``,
1075+
an offset will be used when it will save 4 or more digits. This can
1076+
be controlled 10000 with the new rcParam, ``axes.formatter.offset_threshold``.
1077+
To restore the previous behavior of using an offset to save 2 or more
1078+
digits, use ``rcParams['axes.formatter.offset_threshold'] = 2``.
1079+
1080+
1081+
10711082
``AutoDateFormatter`` format strings
10721083
====================================
10731084

doc/users/whats_new.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,16 @@ New rcparams added
8282
|`ytick.minor.right`, | |
8383
|`ytick.major.right` | |
8484
+---------------------------------+--------------------------------------------------+
85-
|`hist.bins` | the default number of bins to use in |
85+
|`hist.bins` | The default number of bins to use in |
8686
| | `~matplotlib.axes.Axes.hist`. This can be an |
8787
| | `int`, a list of floats, or ``'auto'`` if numpy |
8888
| | >= 1.11 is installed. |
8989
+---------------------------------+--------------------------------------------------+
90-
|`lines.scale_dashes` | If the line dash patterns should scale with |
91-
| | linewidth |
90+
|`lines.scale_dashes` | Whether the line dash patterns should scale with |
91+
| | linewidth. |
92+
+---------------------------------+--------------------------------------------------+
93+
|`axes.formatter.offset_threshold`| Minimum number of digits saved in tick labels |
94+
| | that triggers using an offset. |
9295
+---------------------------------+--------------------------------------------------+
9396

9497

lib/matplotlib/mpl-data/stylelib/classic.mplstyle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ axes.formatter.useoffset : True # If True, the tick label formatter
205205
# to an offset when the data range is very
206206
# small compared to the minimum absolute
207207
# value of the data.
208+
axes.formatter.offset_threshold : 2 # When useoffset is True, the offset
209+
# will be used when it can remove
210+
# at least this number of significant
211+
# digits from tick labels.
208212

209213
axes.unicode_minus : True # use unicode for the minus symbol
210214
# rather than hyphen. See

lib/matplotlib/rcsetup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,7 @@ def validate_animation_writer_path(p):
10861086
# Use the current locale to format ticks
10871087
'axes.formatter.use_mathtext': [False, validate_bool],
10881088
'axes.formatter.useoffset': [True, validate_bool],
1089+
'axes.formatter.offset_threshold': [4, validate_int],
10891090
'axes.unicode_minus': [True, validate_bool],
10901091
'axes.color_cycle': [
10911092
['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728',

lib/matplotlib/tests/test_ticker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def test_SymmetricalLogLocator_set_params():
169169
nose.tools.assert_equal(sym.numticks, 8)
170170

171171

172-
@cleanup
172+
@cleanup(style='classic')
173173
def test_ScalarFormatter_offset_value():
174174
fig, ax = plt.subplots()
175175
formatter = ax.get_xaxis().get_major_formatter()

lib/matplotlib/ticker.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ def __init__(self, useOffset=None, useMathText=None, useLocale=None):
505505

506506
if useOffset is None:
507507
useOffset = rcParams['axes.formatter.useoffset']
508+
self._offset_threshold = rcParams['axes.formatter.offset_threshold']
508509
self.set_useOffset(useOffset)
509510
self._usetex = rcParams['text.usetex']
510511
if useMathText is None:
@@ -689,9 +690,10 @@ def _compute_offset(self):
689690
# are no more than 1 apart at that precision?
690691
oom = 1 + next(oom for oom in itertools.count(oom_max, -1)
691692
if abs_max // 10 ** oom - abs_min // 10 ** oom > 1)
692-
# Only use offset if it saves at least two significant digits.
693+
# Only use offset if it saves at least _offset_threshold digits.
694+
n = self._offset_threshold - 1
693695
self.offset = (sign * (abs_max // 10 ** oom) * 10 ** oom
694-
if abs_max // 10 ** oom >= 10
696+
if abs_max // 10 ** oom >= 10**n
695697
else 0)
696698

697699
def _set_orderOfMagnitude(self, range):

matplotlibrc.template

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,13 @@ backend : $TEMPLATE_BACKEND
318318
# notation.
319319
#axes.formatter.useoffset : True # If True, the tick label formatter
320320
# will default to labeling ticks relative
321-
# to an offset when the data range is very
321+
# to an offset when the data range is
322322
# small compared to the minimum absolute
323323
# value of the data.
324-
324+
#axes.formatter.offset_threshold : 4 # When useoffset is True, the offset
325+
# will be used when it can remove
326+
# at least this number of significant
327+
# digits from tick labels.
325328

326329
# axes.spines.left : True # display axis spines
327330
# axes.spines.bottom : True

0 commit comments

Comments
 (0)
0