Skip to content

[Bug]: Axes.cla() resets axis label position of twin axes #28846

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

Open
StefRe opened this issue Sep 19, 2024 · 5 comments · May be fixed by #28974
Open

[Bug]: Axes.cla() resets axis label position of twin axes #28846

StefRe opened this issue Sep 19, 2024 · 5 comments · May be fixed by #28974

Comments

@StefRe
Copy link
Contributor

StefRe commented Sep 19, 2024

Bug summary

I'm making many plots in a loop using the same Figure and Axes. The basic setup including a twin Axes is done once and the Axes are cleared at the end of the loop using cla().

Axes.cla() resets the label position of the yaxis of the twin Axes to the default (left), resulting in misplaced ylabels in all but the first loops.

Ticks and tick labels of the left y axis are kept at the correct side, so I would the axis label also expect to be kept at the correct side.

Code for reproduction

import matplotlib.pyplot as plt

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
for i in range(2):
    # do some plotting here
    ax2.set(ylabel=f'ylabel of plot # {i}', ylim=(1, i + 2))
    fig.savefig(f'{i}.png')
    ax1.cla()
    ax2.cla()

Actual outcome

1.png:

1

Expected outcome

The expected outcome was produced by adding ax2.yaxis.set_label_position('right') to the loop:

1

Additional information

No response

Operating system

No response

Matplotlib Version

3.9.2

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

None

@StefRe
Copy link
Contributor Author

StefRe commented Sep 19, 2024

I guess an straightforward fix would be to restore the label position after clearing the axis:

for axis in self._axis_map.values():
axis.clear() # Also resets the scale to linear.

by changing line 1274 to

old_label_position = axis.label_position
axis.clear()
axis.label_position = old_label_position

@StefRe
Copy link
Contributor Author

StefRe commented Sep 19, 2024

or better do it in Axis.clear() around:

self._init()

@timhoffm
Copy link
Member

Semi-related: #27825 and the associated PR #28101

@StefRe
Copy link
Contributor Author

StefRe commented Sep 20, 2024

the same pertains to the offset position - it's also reset to the left when clearing a twin x-Axes, which is even more misleading because this error is more difficult to spot:

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax2.cla()
ax2.set_ylim(1e8, 1e8 + 1)

grafik

@timhoffm
Copy link
Member

timhoffm commented Sep 20, 2024

I think this is a duplicate of #28268.

General note: We currently do not have a clear definition (or even implementation) of the semantics of clear. What parts should be reset and what not? See #28851.

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 a pull request may close this issue.

2 participants