-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Fix #21409: Make twin axes inherit parent position #31353
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
Changes from 2 commits
d9ef7af
88d4ec5
a5435b5
ce749b4
5876d06
e8df9e5
c2a343c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4657,6 +4657,9 @@ | |
| twin.set_zorder(self.zorder) | ||
|
|
||
| self._twinned_axes.join(self, twin) | ||
| if not self.get_in_layout(): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is all fine, but needs to be tested.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! I’ll add additional tests to cover this case.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added tests to cover both manual positioning and layout-managed cases. Let me know if anything else should be tested.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer an actual test that the positions match after layout is applied.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, I'll add it
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a test that explicitly checks that the twin and parent positions |
||
| twin.set_position(self.get_position()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the update the not self.get_in_layout() guard looks right and keeps the fix scoped to manual positioning. One small concern: twin.set_position(self.get_position()) sets both original and active positions. If the parent only changed the active position (set_position(..., which="active")), this would also overwrite the twin’s original. Should we consider _set_position(..., which="active") or explicitly using get_position(original=True) depending on intended semantics? |
||
|
|
||
| return twin | ||
|
|
||
| def twinx(self, axes_class=None, **kwargs): | ||
|
|
@@ -4723,7 +4726,7 @@ | |
| axes_class : subclass type of `~.axes.Axes`, optional | ||
| The `.axes.Axes` subclass that is instantiated. This parameter | ||
| is incompatible with *projection* and *polar*. See | ||
| :ref:`axisartist_users-guide-index` for examples. | ||
|
Check warning on line 4729 in lib/matplotlib/axes/_base.py
|
||
|
|
||
| By default, `~.axes.Axes` is used. | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -477,6 +477,16 @@ def test_twin_inherit_autoscale_setting(): | |
| assert not ax_y_off.get_autoscaley_on() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("twin", ("x", "y")) | ||
| def test_twin_respects_position_after_set_position(twin): | ||
| fig, ax = plt.subplots() | ||
|
|
||
| ax.set_position([0.2, 0.2, 0.5, 0.5]) | ||
| ax2 = getattr(ax, f"twin{twin}")() | ||
|
|
||
| assert_allclose(ax.bbox.bounds, ax2.bbox.bounds) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, the new test compares ax.bbox.bounds without forcing a draw; that can be backend‑sensitive. Would you consider comparing ax.get_position().bounds instead, or calling fig.canvas.draw() before comparing bboxes? |
||
|
|
||
|
|
||
| def test_inverted_cla(): | ||
| # GitHub PR #5450. Setting autoscale should reset | ||
| # axes to be non-inverted. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.