Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Description
Bug summary
In a composite figure with nested gridspecs,Figure.align_labels()
(andalign_xlabels()
,align_ylabels()
) can end up aligning labels that should not intuitively be. Likewise withalign_titles()
.
Code for reproduction
fig=plt.figure(figsize=(6,4))gs0=gridspec.GridSpec(nrows=1,ncols=2,figure=fig)gs00=gs0[0].subgridspec(nrows=2,ncols=1,height_ratios=[8,8])gs01=gs0[1].subgridspec(nrows=2,ncols=1,height_ratios=[9,6])left_axs=gs00.subplots()right_axs=gs01.subplots()left_axs[0].set_ylim(0,0.02)# to force nontrivial alignmentleft_axs[0].set_ylabel('foo')left_axs[1].set_ylabel('bar')right_axs[0].set_ylabel('baz')right_axs[1].set_ylabel('qux')left_axs[1].set_title('title')right_axs[1].set_title('title')fig.align_labels()fig.align_titles()
Actual outcome
All labels are aligned. Titles are aligned as well.
Expected outcome
Labels in separate columns are aligned, but labels in different columns should not be. Titles are not aligned:
Additional information
Right now, the ylabel (xlabel) alignment code seems to attempt to align labels on Axes with the same column index (resp. row index) without checking if those indexes are for the same gridspec. To fix this, we should probably add a check that two Axes share the same gridspec (in addition to being in the same row/col) before we align their labels. (This would not allow label alignment across gridspecs, but if a user wants to align labels between two Axes, it seems reasonable to expect them to put the Axes in the same gridspec.)
The same thing happens with align_titles().
For now, a workaround for labels is to callFigure.align_labels()
separately for each sub-gridspec with theaxs
kwarg (as done for the expected outcome figure above).
Operating system
macOS 14.1.1
Matplotlib Version
3.9.2
Matplotlib Backend
module://matplotlib_inline.backend_inline
Python version
3.12.2
Jupyter version
No response
Installation
conda