Skip to content

Fix floating point inaccuracies in axes limits; partial work on offset value selection #5768

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

Closed
wants to merge 1 commit into from

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Dec 31, 2015

(1) plt.plot([-.1, .2]) used to pick (in round numbers mode) [-.1, .25] as
ylims due to floating point inaccuracies; change it to pick [-.1, .2]
(up to floating point inaccuracies).

Note that this requires working around a bug in numpy's implementation
of divmod (numpy/numpy#6127).

Many test images have changed!

See #5767.

The following part is incorrect and only left for reference purposes, please ignore.

(2) Make MaxNLocator only return points within bounds.

e.g. when the xlims are -0.5 .. 10.5, return 0 .. 10 instead of -1
.. 11.

This implies dropping the (unused and deprecated-in-comment)
"trim" keyword argument.

Also slightly clean up the implementation of scale_range.

Initial work on #5738.

@mdboom
Copy link
Member

mdboom commented Dec 31, 2015

(2) Make MaxNLocator only return points within bounds. e.g. when the xlims are -0.5 .. 10.5, return 0 .. 10 instead of -1 .. 11.

Doesn't this imply not displaying all of the data? Or am I missing something?

@mdboom
Copy link
Member

mdboom commented Dec 31, 2015

The twin_axes_spines_top example seems to be a case where data is being cut...

@anntzer anntzer force-pushed the tighter-axes-limits branch from d327e74 to 4fd8a4e Compare January 1, 2016 01:46
@anntzer
Copy link
Contributor Author

anntzer commented Jan 1, 2016

Sorry, please ignore the second part of the original PR message (now edited). This was a left-over of when I thought the xlims were set independently of the ticks. I have also now melded the two commits together as they don't really make sense independently.

Regarding twin_spines_on_top, in fact no, there's no data being cut. The green line reaches 0.40 on the right, and the old image had a y axis going up to 0.45; now it only goes up to 0.40 (re-run the test without thickening the lines to see it).

@@ -168,6 +169,15 @@
long = int


# Work around numpy/numpy#6127.
def divmod(x, y):
Copy link
Member

Choose a reason for hiding this comment

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

This should be private (leading underscore), shouldn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not listed in __all__, just like a bunch of other functions in the same module (closeto, decade_{up,down}, nearest_long, etc.) which are not prefixed by an underscore but still arguably private.

Copy link
Member

Choose a reason for hiding this comment

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

Based on your argument, we would never use underscores. There are two reasons for using one here:

  1. With its present name, this function is shadowing a builtin.
  2. It is a workaround for a bug. Some time after the bug is fixed, we will want to remove this function. If it has an underscore, we don't need to go through a deprecation process, or argue about whether to do so.

Also, I see that divmod is used in 3 other modules; is there reason to use this version in those locations as well? If so, then it could be put in cbook.

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 with @efiring, please add an underscore. There are parts of mpl that pre-date the 'underscore means private' convention in python.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A quick grep through all other calls to divmod in the codebase suggests that they are always called either with integer arguments (in which case there's no bug) or with (nontrivial) array arguments (in which case the fix I used is not enough). So I'll just leave it here (as _divmod) for now.

The ternary-if has also been fixed.

@anntzer
Copy link
Contributor Author

anntzer commented Jan 1, 2016

I'm a bit confused by the Travis failure only for 2.7. Everything passes fine for me (https://travis-ci.org/anntzer/matplotlib/builds/99672606), and Appveyor also seems happy. Thoughts?

@jenshnielsen
Copy link
Member

It's a random failure not related to your work see #5647 (this was the test__EventCollection__set_linestyle.test again)

I have restarted the test

@efiring
Copy link
Member

efiring commented Jan 1, 2016

Apart from my inline comments, this looks like a nice improvement. Thanks for taking it on.

@anntzer anntzer force-pushed the tighter-axes-limits branch from 4fd8a4e to 846fde4 Compare January 1, 2016 21:05

def bin_boundaries(self, vmin, vmax):
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to go through a deprecation cycle for bin_boundaries?

Copy link
Member

Choose a reason for hiding this comment

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

Good question. Can we avoid that simply by renaming _raw_ticks back to bin_boundaries?

Copy link
Member

Choose a reason for hiding this comment

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

On the other hand, we have been saying for a while we want to reduce the size of the public API and this is part of the internal details of how the MaxN locator works so it should be private.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note tha the behavior of _raw_ticks is slightly different from bin_boundaries (namely it handles properly floating-point inaccuracies), so given that strict backwards compatibility has been broken I think it's a good opportunity to make it private. If you prefer I can leave the (now unused) old implementation of bin_boundaries in, with a deprecation warning.

Copy link
Member

Choose a reason for hiding this comment

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

A bug fix (handling floating point better) would not in itself require a name change or deprecation, but I'm perfectly happy with the new private name. To be on the safe side, a deprecation cycle of the old name would be a good idea. (I thought it was being used elsewhere, but I haven't found any such instances.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Restored bin_boundaries with a deprecation warning since v2.0.

@anntzer anntzer force-pushed the tighter-axes-limits branch from 846fde4 to dae0b87 Compare January 2, 2016 03:27
@mdboom
Copy link
Member

mdboom commented Jan 4, 2016

Regarding twin_spines_on_top, in fact no, there's no data being cut. The green line reaches 0.40 on the right, and the old image had a y axis going up to 0.45; now it only goes up to 0.40 (re-run the test without thickening the lines to see it).

Ok. Thanks for confirming.

@mdboom
Copy link
Member

mdboom commented Jan 4, 2016

This is a real nice refinement of a detail that's been broken for a very long time. Thanks for taking this on.

@tacaswell
Copy link
Member

❤️ pep8

======================================================================
FAIL: matplotlib.tests.test_coding_standards.test_pep8_conformance_installed_files
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/travis/build/matplotlib/matplotlib/venv/lib/python3.5/site-packages/nose/case.py", line 198, in runTest
    self.test(*self.arg)
  File "/home/travis/build/matplotlib/matplotlib/lib/matplotlib/tests/test_coding_standards.py", line 249, in test_pep8_conformance_installed_files
    expected_bad_files=expected_bad_files)
  File "/home/travis/build/matplotlib/matplotlib/lib/matplotlib/tests/test_coding_standards.py", line 143, in assert_pep8_conformance
    assert_equal(result.total_errors, 0, msg)
AssertionError: 1 != 0 : Found code syntax errors (and warnings):
/home/travis/build/matplotlib/matplotlib/lib/matplotlib/ticker.py:1405:80: E501 line too long (80 > 79 characters)
----------------------------------------------------------------------

plt.plot([-.1, .2]) used to pick (in round numbers mode) [-.1, .25] as
ylims due to floating point inaccuracies; change it to pick [-.1, .2]
(up to floating point inaccuracies).

Support for the (unused and deprecated-in-comment) "trim" keyword
argument has been dropped as the way ticks are picked has changed.

Many test images have changed!

Implementation notes:
- A bug in numpy's implementation of divmod (numpy/numpy#6127) is worked
around.
- The implementation of scale_range has also been cleaned.

See matplotlib#5767, matplotlib#5738.
@anntzer anntzer force-pushed the tighter-axes-limits branch from dae0b87 to e67c7fc Compare February 15, 2016 21:11
@anntzer
Copy link
Contributor Author

anntzer commented Feb 15, 2016

Fixed and rebased.

@jenshnielsen
Copy link
Member

IMHO this is ready for merge (Needs a rebase which I did in https://github.com/jenshnielsen/matplotlib/tree/tighter-axes-limits) but otherwise looks good

It would be nice to fix issues with numpy 1.11 which this should do. So the question is should we backport this to 2.x or 1.5.x?

@anntzer
Copy link
Contributor Author

anntzer commented Mar 20, 2016

Can you PR the rebase to my branch?

@tacaswell tacaswell closed this Mar 20, 2016
@anntzer anntzer deleted the tighter-axes-limits branch March 20, 2016 23:32
@jenshnielsen
Copy link
Member

For the record this was merged via #6192 (which is just a rebase onto master of the time)

@QuLogic QuLogic added this to the 2.0 (style change major release) milestone Apr 3, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants