Position of two subplots with bar and plot charts

조회 수: 6 (최근 30일)
Yaser Khojah
Yaser Khojah 2021년 1월 31일
댓글: Yaser Khojah 2021년 2월 3일
Hello, I am trying to align two subplots; a bar and a bar plots. Thus, I used position but I still find them not alighted. Could you please help
x_value = [1;2;3;4];
Analyti = [9343300000.00000;9343300000.00000;9343300000.00000;9343300000.00000];
NPV_all = [10098480743.4960, 9405675708.82828, 9345356723.26658, 9343358602.05035];
period = [0.109958329319958 0.00747830013942861 0.0753802706146702 1.68890524065252];
figure
subplot(2,1,1)
ylabel('NPV ($B)')
plot(x_value, Analyti /10^9,'--k'); hold on
p1 = plot(x_value, NPV_all'/10^9); hold on
hp = gca;
posn1 = hp.Position;
title('Impact of Discretizaion on NPV')
legend('Discrete NPV','continuous NPV', 'Analytical NPV')
xlabel('Discrete Time Method')
xticks([1:4])
xticklabels({'Years','Months','Days','Hours'})
ylim([8,11])
subplot(2,1,2)
p2 = bar(period); hold on
xticks([1:4])
xticklabels({'Years','Months','Days','Hours'})
ylabel('Seconds')
xlabel('Discrete Time Method')
hp2 = gca;
posn2 = hp2.Position;
set(hp2,'Position',[posn2(1:2), posn1(3:4)])

채택된 답변

dpb
dpb 2021년 1월 31일
편집: dpb 2021년 1월 31일
You're messing with the wrong thing -- it's xlim you want to make the same...
...
figure
hAx(1)=subplot(2,1,1); % save the handle for later when create the axes...
ylabel('NPV ($B)')
plot(x_value, Analyti /10^9,'--k');
hold on
p1 = plot(x_value, NPV_all'/10^9);
title('Impact of Discretizaion on NPV')
legend('Discrete NPV','continuous NPV', 'Analytical NPV')
xlabel('Discrete Time Method')
xticks([1:4])
xticklabels({'Years','Months','Days','Hours'})
ylim([8,11])
hAx(2)=subplot(2,1,2);
p2 = bar(period); hold on
xticks([1:4])
xticklabels({'Years','Months','Days','Hours'})
ylabel('Seconds')
xlabel('Discrete Time Method')
xlim(hAx(1),hAx(2).XLim) % put both plots on same x-axis range
You could make it somewhat easier if you were to use a categorical variable for x instead...
...
periods={'Years','Months','Days','Hours'};
x_value=categorical([1:numel(periods)],[1:numel(periods)],periods,'Ordinal',1);
figure
hAx(1)=subplot(2,1,1);
hL(1)=plot(x_value, Analyti /10^9,'--k'); hold on
hL(2)=plot(x_value, NPV_all'/10^9);
title('Impact of Discretizaion on NPV')
legend('Discrete NPV','continuous NPV', 'Analytical NPV')
xlabel('Discrete Time Method')
ylabel('NPV ($B)')
ylim([8,11])
hAx(2)=subplot(2,1,2);
hBar=bar(x_value,period);
ylabel('Seconds')
xlabel('Discrete Time Method')
  댓글 수: 1
Yaser Khojah
Yaser Khojah 2021년 2월 3일
Thank you so mcuh for your help. It is really useful :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by