One common xlabel and ylabel for multiple subplots

1.262 visualizaciones (últimos 30 días)
Al_G
Al_G el 10 de En. de 2020
Comentada: Changbo Yu el 11 de En. de 2024
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.

Respuesta aceptada

Subhadeep Koley
Subhadeep Koley el 13 de En. de 2020
Editada: Subhadeep Koley el 30 de Dic. de 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 comentarios
志鹏 陈
志鹏 陈 el 26 de Dic. de 2023
Thanks!!!
Changbo Yu
Changbo Yu el 11 de En. de 2024
Thank you!

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 30 de Dic. de 2020
If you have R2018b or later, use sgtitle().
  4 comentarios
Stephen
Stephen el 27 de Sept. de 2022
Ughhhhhh
Image Analyst
Image Analyst el 27 de Sept. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Labels and Annotations en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by