You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: twinx and twiny ignores previous set_position #21409
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.
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
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.
This is not solved yet. When I turn off tick params of specific axis above in my code and twiny that axis below, tick params of the original axis are shown in my plot. That is why, I do set_xlabels([]) and turn off tick params later in my code as well like in the code below:
Hi! I’ve been looking into this issue and the related PRs (#21413 and #22817) to understand why they stalled. (As it was told me on the Incubator chat)
From what I understand, the original issue occurs when set_position() is called before twinx() / twiny(). In that case, the twin axes can end up using the original subplot position rather than the current position of the parent axes, so the two axes are no longer aligned.
It seems that #21413 attempted to address this by always locating the twin from the parent axes, regardless of whether the parent came from a SubplotSpec or not. However, the discussion suggests that this approach ran into problems with tight_layout, since subplot twins currently rely on SubplotSpec-based layout behavior.
Then #22817 took a more conservative approach and ensured that if the parent axes is moved after the twin is created, the twin follows. But as noted in the PR, the original case from the issue (moving the axes before creating the twin) would still not be resolved.
I’m currently exploring whether it might be possible to keep the existing subplot-based layout behavior, but explicitly synchronize the positions between twinned axes (both at creation time and when set_position() is called). The idea would be to keep compatibility with layout engines while ensuring that twins always start from the current geometry of the parent.
Does this sound like a reasonable direction to investigate?
Activity
jklymak commentedon Oct 21, 2021
Agreed - the fix is relatively simple (I think)
mahammadodj commentedon Sep 12, 2023
This is not solved yet. When I turn off tick params of specific axis above in my code and twiny that axis below, tick params of the original axis are shown in my plot. That is why, I do set_xlabels([]) and turn off tick params later in my code as well like in the code below:
Matplotlib version: 3.7.1
Python version: 3.11.4
maf2310 commentedon Mar 13, 2026
Hi! I’ve been looking into this issue and the related PRs (#21413 and #22817) to understand why they stalled. (As it was told me on the Incubator chat)
From what I understand, the original issue occurs when
set_position()is called beforetwinx()/twiny(). In that case, the twin axes can end up using the original subplot position rather than the current position of the parent axes, so the two axes are no longer aligned.It seems that #21413 attempted to address this by always locating the twin from the parent axes, regardless of whether the parent came from a
SubplotSpecor not. However, the discussion suggests that this approach ran into problems withtight_layout, since subplot twins currently rely onSubplotSpec-based layout behavior.Then #22817 took a more conservative approach and ensured that if the parent axes is moved after the twin is created, the twin follows. But as noted in the PR, the original case from the issue (moving the axes before creating the twin) would still not be resolved.
I’m currently exploring whether it might be possible to keep the existing subplot-based layout behavior, but explicitly synchronize the positions between twinned axes (both at creation time and when
set_position()is called). The idea would be to keep compatibility with layout engines while ensuring that twins always start from the current geometry of the parent.Does this sound like a reasonable direction to investigate?
Fix matplotlib#21409: Make twin axes inherit parent position
Merge pull request #31353 from maf2310/fix-twin-position