Position of two subplots with bar and plot charts
6 views (last 30 days)
Show older comments
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)])
0 Comments
Accepted Answer
dpb
on 31 Jan 2021
Edited: dpb
on 31 Jan 2021
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')
More Answers (0)
See Also
Categories
Find more on Subplots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!