필터 지우기
필터 지우기

Align subplot x-axes

조회 수: 13 (최근 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 CenterFile Exchange에서 2-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