One common xlabel and ylabel for multiple subplots

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

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

Thanks, it works fine! Although depending your font size of the X,Y ticks the labels have to be shifted.
This works!
Even with tight_subplots, this method worked, while other solutions like suplabels did not!
Subhadeep Koley; how would this be different if I have double y axis? I have a 3x1 subplot created in a loop by (3,1,i), for i =1:3. In the same loop I have
yyaxis left
xlabel('')
ylabel('')
yyaxis right
ylabel('')
I want one common left ylabel, right ylabel and xlabel.
Henning Eimstad The below code might help!
close all;
fig = figure;
% Plot your subplots here
for idx = 1:3
subplot(3, 1, idx); plot(rand(10));
end
% Give common xlabel, ylabel and title to your figure
% Create a new axis
ax = axes(fig);
% Specifiy the active side for the axes ax
yyaxis(ax, 'left');
% Specify visibility of the current axis as 'off'
han = gca;
han.Visible = 'off';
% Specify visibility of Title, XLabel, and YLabel as 'on'
han.Title.Visible = 'on';
han.XLabel.Visible = 'on';
han.YLabel.Visible = 'on';
% Give title, xlabel and left ylabel
title('Title')
xlabel('X Label')
ylabel('Left Y Label')
% Specifiy the active side for the axes ax
yyaxis(ax, 'right');
% Give right ylabel
ylabel('Right Y Label')
% Specify visibility of YLabel as 'on'
han.YLabel.Visible = 'on';
how to postion this common xlabel and ylabel? I want to shift the commona yLabel slightly to the left and xlabel slightly downward.
@Vivek, edit plot option may help. Go to-- figure>tools>edit plot> place the cursor on the one you want to move or
you can find edit plot option directly from the tool bar, it looks like cursor.
Hope that helps!
Super simple and nice solution! Thanks a lot

Sign in to comment.

More Answers (1)

If you have R2018b or later, use sgtitle().

4 Comments

This solution does not solve the axis labelling problem.
i'm not sure why MathWorks would implement a solution to labeling a set of plots titles at once, but not their axes (in the case they are arranged to imply a common axis)
@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

Asked:

on 10 Jan 2020

Commented:

on 11 Jan 2024

Community Treasure Hunt

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

Start Hunting!