Align subplot x-axes

17 ビュー (過去 30 日間)
Jeff
Jeff 2018 年 4 月 24 日
編集済み: Ameer Hamza 2018 年 4 月 24 日
Hi all, could someone please help me on how to align the scales?
% data data = rand(14,2); x = data(:,1); y = data(:,2);
% dates t1 = datetime(2016,5,13); t2 = datetime(2016,8,12); t = t1:calweeks(1):t2;
% plot h = figure; graph1 = subplot(2,1,1); plot(t,x,'b-o',t,y,'r-*'); graph1.XAxis.Limits = [t1 t2]; graph1.XAxis.TickValues = t'; graph1.XAxis.TickLabelFormat = 'MMM-dd'; xtickangle(45) % graph1.XAxis.Visible = 'off'; % axes('Color','none','XColor','none'); graph2 = subplot(2,1,2); bar(t,x-y);
I would like to have the axes aligned like shown in the graph but the panels to be equal size.
Thank-you in advance.

採用された回答

Jeff
Jeff 2018 年 4 月 24 日
編集済み: Jeff 2018 年 4 月 24 日
I found a solution, realizing later that i didn't fit the graph 2 axis like I did for graph 1, and it seems to have solved the issue. Best regards, Jeff
% data data = rand(14,2); x = data(:,1); y = data(:,2);
% dates t1 = datetime(2016,5,13); t2 = datetime(2016,8,12); t = t1:calweeks(1):t2;
% plot h = figure; graph1 = subplot(2,1,1); plot(t,x,'b-o',t,y,'r-*'); graph1.XAxis.Limits = [t1 t2]; graph1.XAxis.TickValues = t'; graph1.XAxis.TickLabelFormat = 'MMM-dd'; xtickangle(45) graph1.XAxis.Visible = 'off';
graph2 = subplot(2,1,2); bar(t,x-y); graph2.XAxis.Limits = [t1 t2]; graph2.XAxis.TickValues = t'; grap2.XAxis.TickLabelFormat = 'MMM-dd';
  1 件のコメント
Ameer Hamza
Ameer Hamza 2018 年 4 月 24 日
編集済み: Ameer Hamza 2018 年 4 月 24 日
You can do this, but it will cut the width of first and last bar in half.

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

その他の回答 (1 件)

Ameer Hamza
Ameer Hamza 2018 年 4 月 24 日
The axis handle gave a Position property which specifies its location on the figure. The property is defined as [left bottom width height] as described here. Since you are only interested in changing the left starting position and width of graph2 you just need to change 1st and 3rd element of graph2.Position. You can set these values according to your requirement. As an example
graph2.Position(1) = graph1.Position(1)-0.04; % you can experiment by changing 0.04 and 0.08
graph2.Position(3) = graph1.Position(3)+0.08; % unless you achieve desired result.
You can just add these two statements at end of your script. Here I am moving left starting position of graph2 a bit left as compared to graph1 and also changing its width so that the figure will expand symmetrically.

カテゴリ

Help Center および File ExchangeSubplots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by