One common xlabel and ylabel for multiple subplots

1 260 views (last 30 days)
Is there a straightforward way to add one common x label and ylabel to a figure containing multiple subplots?
The solutions I read so far require a file exchange function or a fixed number of subplots, and my number of subplots ranges from 5 to 10 (generally in one column).
I'm imagining there must be a way to determine the overall figure size, regardless of the number of subplots, and center a single xlabel and ylabel on each axis of the larger figure.

Accepted Answer

Subhadeep Koley
Subhadeep Koley on 13 Jan 2020
Edited: Subhadeep Koley on 30 Dec 2020
Hi, the example code below adds one common xlabel and ylabel to a figure containing multiple subplots, irrespective of the number of subplots.
close all;clc;
fig = figure;
% Plot your subplots here
subplot(2,3,1); plot(rand(5));
subplot(2,3,2); plot(rand(5));
subplot(2,3,3); plot(rand(5));
subplot(2,3,4); plot(rand(5));
subplot(2,3,5); plot(rand(5));
subplot(2,3,6); plot(rand(5));
% Give common xlabel, ylabel and title to your figure
han=axes(fig,'visible','off');
han.Title.Visible='on';
han.XLabel.Visible='on';
han.YLabel.Visible='on';
ylabel(han,'yourYLabel');
xlabel(han,'yourXLabel');
title(han,'yourTitle');
Hope this helps!
EDIT: For MATLAB R2019b or above, using tiledlayout(__) would be simpler over subplot. Like below,
% Create a tiledlayout
figure
t = tiledlayout('flow');
% Plot in tiles
nexttile, plot(rand(5))
nexttile, plot(rand(5))
nexttile, plot(rand(5))
nexttile, plot(rand(5))
nexttile, plot(rand(5))
nexttile, plot(rand(5))
% Specify common title, X and Y labels
title(t, 'Common title')
xlabel(t, 'Common X label')
ylabel(t, 'Common Y label')
  10 Comments

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 30 Dec 2020
If you have R2018b or later, use sgtitle().
  4 Comments
Image Analyst
Image Analyst on 27 Sep 2022
@Charles Daigle I'm not sure what you mean. You can give a title to each axes with title. Perhaps if you posted a screenshot. Do you mean that you don't want each y axis to have it's own label and you want a single y label for, say, a stack of 10 plots? You know you can just have no label and use text to put up a vertical label to the left of all your plots positioned and rotated however you like.

Sign in to comment.

Categories

Find more on Labels and Annotations in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!