One common xlabel and ylabel for multiple subplots

1 294 vues (au cours des 30 derniers jours)
Al_G
Al_G le 10 Jan 2020
Commenté : Changbo Yu le 11 Jan 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.

Réponse acceptée

Subhadeep Koley
Subhadeep Koley le 13 Jan 2020
Modifié(e) : Subhadeep Koley le 30 Déc 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 commentaires
志鹏 陈
志鹏 陈 le 26 Déc 2023
Thanks!!!
Changbo Yu
Changbo Yu le 11 Jan 2024
Thank you!

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 30 Déc 2020
If you have R2018b or later, use sgtitle().
  4 commentaires
Stephen
Stephen le 27 Sep 2022
Ughhhhhh
Image Analyst
Image Analyst le 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Labels and Annotations dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by