Position of two subplots with bar and plot charts

2 ビュー (過去 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 Center および File ExchangeSubplots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by