Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Reuse InsetLocator to make twinned axes follow their parents. #18456

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 1 commit into from
Sep 15, 2020

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Sep 11, 2020

From a positioning PoV, a twinned axes is just like an inset axes whose
position exactly matches the parent's position. Doing so removes the
need for the heuristic in Figure.subplots_adjust/GridSpec.update
where a non-gridspec-managed Axes would track the position of any Axes
with which it shared either xaxis or yaxis, which was a proxy for
twinning (per 721b949). This would cause incorrect behavior in rare
cases such as

from pylab import *
ax = subplot(221)
axes([.6, .6, .3, .3], sharex=ax)
subplots_adjust(left=0)

where the subplots_adjust call would make the second axes go on top of
the first.

PR Summary

PR Checklist

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (run flake8 on changed files to check).
  • New features are documented, with examples if plot related.
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • Conforms to Matplotlib style conventions (install flake8-docstrings and pydocstyle<4 and run flake8 --docstring-convention=all).
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).

@anntzer anntzer added this to the v3.4.0 milestone Sep 11, 2020
Copy link
Contributor

@dopplershift dopplershift left a comment

Choose a reason for hiding this comment

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

Looks reasonable. Can you add your sample code as a test?

@jklymak
Copy link
Member

jklymak commented Sep 11, 2020

I think this idea is great, but can we rename _InsetLocator to something more general to make it clear that its used in a few places? _ChildAxesLocator? Also, I agree a test would be nice....

@anntzer
Copy link
Contributor Author

anntzer commented Sep 12, 2020

I renamed the thing to TransformedBoundsLocator, given that it locates an axes using tranformed bbox bounds.
I think the likelihood of accidentally going back to the old behavior is pretty close to zero (you'd need to remove the locator, and put back elsewhere the code for manually making the twinned axes follow their parent, because otherwise you'd fail current twin axes positioning tests), so I'd say we could just skip the test.

@jklymak
Copy link
Member

jklymak commented Sep 12, 2020

The tests help codify expected behaviour. If someone rewrites all of this from scratch it would be nice if this behaviour would be explicitly tested.

@anntzer
Copy link
Contributor Author

anntzer commented Sep 12, 2020

My point is that the behavior is already codified in all the tests for twinned axes, and that it would really require conscious malfeasance :-) to go back to the old behavior (you need to put back the manual position tracking).

@jklymak
Copy link
Member

jklymak commented Sep 13, 2020

? you have a case that was broken for a previous implementation. This implementation presumably fixes that case, so having a test would be preferable.

@anntzer
Copy link
Contributor Author

anntzer commented Sep 13, 2020

It was broken because of some rather roundabout way of trying to keep track of twinning (by equating twinning with sharing), which I am arguing is very unlikely to be reintroduced.

@dopplershift
Copy link
Contributor

@anntzer You've been able to change library behavior without a test failing. That's bad. Therefore, we should have a test. Given that the test should be all of 6 lines, that doesn't seem like a huge maintenance burden for the project to assume.

@anntzer
Copy link
Contributor Author

anntzer commented Sep 14, 2020

sure...

@@ -6513,3 +6513,12 @@ def test_multiplot_autoscale():
ax2.axhspan(-5, 5)
xlim = ax1.get_xlim()
assert np.allclose(xlim, [0.5, 4.5])


def test_sharing_does_not_link_positions():
Copy link
Member

Choose a reason for hiding this comment

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

does not or does? I'm confused, as the final assert checks for equality.

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 checking that the position of the shared axes did not change (i.e. that it did not get linked to the position of the sharee).

From a positioning PoV, a twinned axes is just like an inset axes whose
position exactly matches the parent's position.  Doing so removes the
need for the heuristic in `Figure.subplots_adjust`/`GridSpec.update`
where a non-gridspec-managed Axes would track the position of any Axes
with which it shared either xaxis or yaxis, which was a proxy for
twinning (per 721b949).  This would cause incorrect behavior in rare
cases such as
```python
from pylab import *
ax = subplot(221)
axes([.6, .6, .3, .3], sharex=ax)
subplots_adjust(left=0)
```
where the `subplots_adjust` call would make the second axes go on top of
the first.
@jklymak jklymak merged commit 61807e1 into matplotlib:master Sep 15, 2020
@anntzer anntzer deleted the twinpos branch September 15, 2020 14:27
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.

4 participants