Common X label for 2x2 subplots without using for loop?

56 ビュー (過去 30 日間)
abdur rauf
abdur rauf 2018 年 8 月 14 日
コメント済み: Oluwafemi Ojo 2020 年 3 月 25 日
Hello everyone,
I have created 4 subplots with 2 rows and 2 columns. The handles for subplots are h1, h2, h3, and h4. I want to use common X and Y-label for all subplots. For Y-label, I used the following code:
p1=get(h1,'position');
p2=get(h2,'position');
p3=get(h3,'position');
p4=get(h4,'position');
height=p1(2)+p1(4)-p4(2);
h5=axes('position',[p3(1) p3(2) p3(3) height],'visible','off');
I got the Y-label at the desired location. Is there any way to give common X-label to these subplots?
Any help will be appreciated.
Thanks

採用された回答

jonas
jonas 2018 年 8 月 14 日
編集済み: Adam Danz 2019 年 12 月 18 日
Here is how you can set a single centered x- and y-label on a 2x2 plot.
h1=subplot(2,2,1);
h2=subplot(2,2,2);
h3=subplot(2,2,3);
h4=subplot(2,2,4);
p1=get(h1,'position');
p2=get(h2,'position');
p3=get(h3,'position');
p4=get(h4,'position');
height=p1(2)+p1(4)-p4(2);
width=p4(1)+p4(3)-p3(1);
h5=axes('position',[p3(1) p3(2) width height],'visible','off');
h5.XLabel.Visible='on'
h5.YLabel.Visible='on'
axes(h5)
ylabel('test')
xlabel('test')
...and as a bonus, here is how you can set the same xlabel on all plots, without a for loop
h1=subplot(1,2,1)
h2=subplot(1,2,2)
h=findobj(gcf,'type','axes')
set([h.XLabel],'string','XLabel')
  2 件のコメント
abdur rauf
abdur rauf 2018 年 8 月 14 日
@ jonas,
Thank you.
Oluwafemi Ojo
Oluwafemi Ojo 2020 年 3 月 25 日
Please, how do I do this for a 1 by 2 plot especially when I'm using tight subplot. Also, how does setting the height and width work?

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

その他の回答 (1 件)

Andy Su
Andy Su 2020 年 1 月 30 日
And for a common y label for a right hand y axis too please?

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by