Explain Codes LogoExplain Codes Logo

Common xlabel/ylabel for matplotlib subplots

python
matplotlib
plotting
data-visualization
Anton ShumikhinbyAnton Shumikhin·Feb 2, 2025
TLDR

The strategy to apply uniform xlabel or ylabel for all matplotlib subplots, involves using fig.supxlabel('X Label') and fig.supylabel('Y Label') once you have crafted your figure with subplots via plt.subplots(). This approach neatly centralizes your axis labels offering a clean looking graph layout.

import matplotlib.pyplot as plt fig, axs = plt.subplots(2, 2) fig.supxlabel('Common X Label') fig.supylabel('Common Y Label') plt.show()

Ensure your matplotlib version >= 3.4.0 to utilize supxlabel and supylabel. If not, leverage fig.text() as a strategy:

fig.text(0.5, 0.02, 'Common X Label', ha='center') fig.text(0.04, 0.5, 'Common Y Label', ha='center', rotation='vertical')

By setting ha='center' your text alignment is neatly centred. Adjust your fig.text() coordinates to ffine-tune your label positions.

Going farther: Position control and layout spacing

Sometimes, subplots need more than shared labels; they ask for individual attention. For example, applying sharex and sharey parameters can provide synchronized sharing of axes limits and ticks amongst subplots, but providing individual tweaks ensures no label overlap and well-spaced axes, therefore upgrading your plot's readability.

Axes and ticks: How to sync?

While creating subplots, synchronizing the axes involves:

fig, axs = plt.subplots(2, 2, sharex=True, sharey=True)

Layout adjustment: Get rid of overlaps

Prevent label overlap neatly, this works even with variable subplot sizes:

fig.tight_layout() # Or set constrained_layout=True in plt.subplots()

More control over your subplots

Look towards gridspec for refined control over subplot sizes and arrangements:

fig.add_gridspec(nrows, ncols)

Ticks: How to customize?

To refine your axis tick labels:

plt.tick_params(axis='both', which='major', labelsize=10) # "major" ticks example, because "size does matter" :)

Advanced subplot manipulation

Subplot labels: Making them unique

Assign unique labels to each subplot using set_xlabel and set_ylabel:

axs[0, 1].set_xlabel('Subplot-specific X Label') # "Because every subplot deserves to be unique" :)

The power of hidden axes

Create a large hidden axis that acts as a neat background to your subplots:

fig.add_subplot(111, frame_on=False) plt.tick_params(labelcolor='none', top=False, bottom=False, left=False, right=False) # Playing "hide and seek" with tick labels!

gridspec: Enhanced layout formation

Use add_gridspec to create grids of subplots:

gs = fig.add_gridspec(2, 2) ax1 = fig.add_subplot(gs[0, :]) # "Because some plots just need more space"

Aesthetics? Use tight_layout

Ensure your subplots are neat and attractive:

fig.tight_layout() # "No one likes a sloppy subplot!" :)

Dynamic visualization

Flaunt data by embedding images within your subplots:

axs[0, 0].imshow(image_data) # A picture on subplot is worth a thousand numbers!

A central unifying title

A central title resonates with the big picture. Add a caption to your data collection with fig.suptitle:

fig.suptitle('Gallery of Data') # "Welcome to my data exhibit!"

Tweaking subplots: Individually

Access specific subplots for unique data narratives:

axs[1, 0].set_title('Detailed View') # "Let's dive in for a closer look!"

Subplots may be standalone data stories, but xlabel and ylabel unify these tales into a cogent narrative.