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]: Axes.cla() resets axis label position of twin axes #28846
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
importmatplotlib.pyplotaspltfig, ax1=plt.subplots()
ax2=ax1.twinx()
foriinrange(2):
# do some plotting hereax2.set(ylabel=f'ylabel of plot # {i}', ylim=(1, i+2))
fig.savefig(f'{i}.png')
ax1.cla()
ax2.cla()
Actual outcome
1.png:
Expected outcome
The expected outcome was produced by adding ax2.yaxis.set_label_position('right') to the loop:
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:
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.
Activity
StefRe commentedon Sep 19, 2024
I guess an straightforward fix would be to restore the label position after clearing the axis:
matplotlib/lib/matplotlib/axes/_base.py
Lines 1273 to 1274 in a254b68
by changing line 1274 to
StefRe commentedon Sep 19, 2024
or better do it in Axis.clear() around:
matplotlib/lib/matplotlib/axis.py
Line 892 in a254b68
timhoffm commentedon Sep 19, 2024
Semi-related: #27825 and the associated PR #28101
StefRe commentedon 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:
timhoffm commentedon 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.Add test.py to test the various issues linked to the matplotlib#28268 …