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

Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
14 changes: 14 additions & 0 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4657,6 +4657,20 @@ def _make_twin_axes(self, *args, **kwargs):
twin.set_zorder(self.zorder)

self._twinned_axes.join(self, twin)

# If the parent Axes has been manually positioned (set_position() sets
# in_layout=False), the SubplotSpec-based add_subplot(...) path ignores
# that manual position when creating a twin. In that case, explicitly
# copy both the original and active positions to the twin so they start
# aligned.
#
# For layout-managed Axes (in_layout=True), we keep the existing
# SubplotSpec-driven behavior, so layout engines such as tight_layout
# and constrained_layout continue to control positioning.
if not self.get_in_layout():
Comment thread
maf2310 marked this conversation as resolved.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this is all fine, but needs to be tested.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I’ll add additional tests to cover this case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ok, I'll add it

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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
still match after tight_layout() is applied.
Let me know if I should add anything else.

twin._set_position(self.get_position(original=True), which="original")
twin._set_position(self.get_position(original=False), which="active")

return twin

def twinx(self, axes_class=None, **kwargs):
Expand Down
10 changes: 10 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.get_position().bounds, ax2.get_position().bounds)


def test_inverted_cla():
# GitHub PR #5450. Setting autoscale should reset
# axes to be non-inverted.
Expand Down
Loading