Skip to content

[Bug]: twinx and twiny ignores previous set_position #21409

@bfield1

Description

@bfield1

Bug summary

If I manually set the position of an Axes, then call twinx or twiny, the new twinned Axes appears in the original position of the parent Axes, not in the new position.

Code for reproduction

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.set_position([0.2,0.2,0.5,0.5])
ax2 = ax.twinx()
plt.show()

Actual outcome

MPL_Bug_Image

I note that ax.get_position(original=True) and ax.get_position(original=False) both give the same answer.
I also note that the same issue occurs for ax.twiny().

Expected outcome

MPL_Bug_Expected
This expected outcome can be obtained by adding the line

ax2.set_position(ax.get_position())

after calling ax2=ax.twinx(). Alternatively, it can be obtained by calling ax.set_position after calling ax.twinx.
However, I would expect twinx to automatically inherit the position of the parent Axes, rather than draw the new Axes in an unrelated location.

Operating system

Windows 10

Matplotlib Version

3.4.2

Matplotlib Backend

TkAgg

Python version

3.9.4

Jupyter version

No response

Other libraries

No response

Installation

pip

Conda channel

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions