Skip to content

STY: in ScalarFormatter, use offset to save at least 4 digits, not 2 #7365

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 3 commits into from
Nov 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions doc/users/dflt_style_changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

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

unclear what you mean by save...maybe "will be used when the tick values have 4 or more digits"

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree that it is hard to figure out how to describe this, but it is not a matter of the number of digits the tick values have, it is a matter of how many fewer digits they can have if the offset is used. That's what is meant by "saved". I am using the original terminology of @anntzer from his comment in the code, I believe--though maybe that comment predates his refactoring.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, maybe then use retain instead? Same basic meaning, but doesn't have associations with saving a figure.

Copy link
Contributor

Choose a reason for hiding this comment

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

I introduced that terminology (see last chunk of https://github.com/matplotlib/matplotlib/pull/5785/files). It's not "retain", it's the leading digits that are not retained thanks to the use of the offset (i.e., exactly what @efiring said).

Copy link
Member

Choose a reason for hiding this comment

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

I'm so thoroughly confused since save is usually synonymous with retain and yet it's apparently not in this case and I'm not quite sure what it is a matter of how many fewer digits they can have if the offset is used. means in a practical sense.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I meant an example of what setting the offset does to the plot. Like I went back to the original issue and the offset code changed [1999-2000] changed the ticks to -10-10, but I don't know what the generic form of that is...basically, I'm also wondering if it makes sense to explain this functionality in terms of what it saves.

Copy link
Member

Choose a reason for hiding this comment

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

Oh sorry. After writing out a description (as best as I could understand it), I'm not sure whether it will help clarify the docs, but maybe I'm just thinking along the wrong lines.

Let's just say there are two ticks, which can be written as abcd and efgh (where each of those letters is a single 0-9 digit). Then pick some other value between them jk00 so that the ticks are abcd - jk00 => -nopq and efgh - jk00 => rstu. If you can pick jk00 such that n == o == r == s == 0, then the offset is used.

The new setting means the "other value" must be jklm and additional conditions of p == q == t == u == 0 must also be satisfied (which it wouldn't be possible in this case, thus fixing dates.)

Copy link
Member

Choose a reason for hiding this comment

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

No worrys, as it sounds complicated. Is there a typical use case for this sort of offsetting or a name for it? (Like it's not scientific notation, which is also partly why it's so confusing)

It seems like it encodes ticks to reduce their size by offset or more digits.

Copy link
Member Author

Choose a reason for hiding this comment

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

Example: an axis is showing years from 1990 through 2010. This could be shown with ticks from -10 to 10 with an offset of 2000. That is classic mode: 2 digits can be chopped off of the tick labels, so the offset is used if useoffset is True. With the new setting, saving those two digits in the tick labels is not enough, so the offset won't be used, and users will be happier. But if you have an axis from 20001 to 20003, then it will be given an offset of 20000, and the ticks will go from 1 to 3. The tick labels have been shortened by 4 digits, so they will have a better chance of fitting in the frame without overlapping.

A version of this was put in place long ago. Its purpose is to allow mpl to handle a wider range of cases by default, without excessively long tick labels.

Copy link
Member

@story645 story645 Nov 3, 2016

Choose a reason for hiding this comment

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

While that explanation is too long in docs, it's by far the clearest. Thanks!

So I think it's something like "Computes offset when ticks have more significant digits than offset_threshold. Displays tick-offset at each tick location to reduce tick label size to offset_threshold."

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
====================================

Expand Down
9 changes: 6 additions & 3 deletions doc/users/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
+---------------------------------+--------------------------------------------------+


Expand Down
4 changes: 4 additions & 0 deletions lib/matplotlib/mpl-data/stylelib/classic.mplstyle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/tests/test_ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def test_SymmetricalLogLocator_set_params():
nose.tools.assert_equal(sym.numticks, 8)


@cleanup
@cleanup(style='classic')
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this the default style already?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes for image_comparison, but no for cleanup. It looks to me like that is a bug in cleanup.

Copy link
Member

Choose a reason for hiding this comment

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

I am having dejavu about this and some gordian knot we failed to deal with like 6mo ago....

Copy link
Member Author

Choose a reason for hiding this comment

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

Fortunately, it doesn't seem to matter much. style='classic' is needed two places in test_ticker, and a few more in the specific backend testers. The tests are working as they are. I don't think it is worth spending any time on right now.

def test_ScalarFormatter_offset_value():
fig, ax = plt.subplots()
formatter = ax.get_xaxis().get_major_formatter()
Expand Down
6 changes: 4 additions & 2 deletions lib/matplotlib/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
7 changes: 5 additions & 2 deletions matplotlibrc.template
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down