One common xlabel and ylabel for multiple subplots

1,205 ビュー (過去 30 日間)
Al_G
Al_G 2020 年 1 月 10 日
コメント済み: Changbo Yu 2024 年 1 月 11 日
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.

採用された回答

Subhadeep Koley
Subhadeep Koley 2020 年 1 月 13 日
編集済み: Subhadeep Koley 2020 年 12 月 30 日
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 件のコメント
志鹏 陈
志鹏 陈 2023 年 12 月 26 日
Thanks!!!
Changbo Yu
Changbo Yu 2024 年 1 月 11 日
Thank you!

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2020 年 12 月 30 日
If you have R2018b or later, use sgtitle().
  4 件のコメント
Stephen
Stephen 2022 年 9 月 27 日
Ughhhhhh
Image Analyst
Image Analyst 2022 年 9 月 27 日
@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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLabels and Annotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by