How can I stack two bars next to each other on top of a single bar in a bar graph?
20 views (last 30 days)
Show older comments
Luca van Opdorp
on 20 Dec 2023
Commented: Luca van Opdorp
on 22 Dec 2023
I am trying to make a balanced field length graph (see image below):

How can I stack the two bars next to each other on top of the 'distance V1'
I tried using the options 'stacked' and 'grouped' seperately but this didn't work.
My code is below:
S = 845;
mu = 0.015;
g = 9.81;
T1 = 360000;
T3 = 360000*3;
T4 = 360000*4;
CDt= 0.038;
CLt = 0.6;
rho = 1.275;
V1 = linspace(0,75,15);
VR = linspace(0,77,50);
W5 = 575000*9.81;
T0 = 0;
mub = 0.40;
p = g*((T4/W5)-mu);
q = -((g*(CDt-mu*CLt)*rho*S)/(2*W5));
sv1 = 1/(2*q)*(log(p+q*V1.^2)-log(p)); % distance to accelerate to v1
Vb = linspace(0,75,15);
pb = g*((T0/W5)-mub);
qb = -((g*(CDt-mub*CLt)*rho*S)/(2*W5));
svb = (1/(2*qb)*(log(pb)-log(pb+qb*Vb.^2))); % distance to decelerate from v1
p34 = g*((T3/W5)-mu);
q34 = -((g*(CDt-mu*CLt)*rho*S)/(2*W5));
Vlof = 90-V1;
sv34 = 1/(2*q34).*(log(p34+q34*Vlof.^2)-log(p34));
Z = [svb;sv34];
z_trans = transpose(Z);
bar(z_trans); % distance to V1
grid on
bar(sv1,'stacked')
hold off
Accepted Answer
Dyuman Joshi
on 22 Dec 2023
Here's the visualization of the approach I mentioned -
S = 845;
mu = 0.015;
g = 9.81;
T1 = 360000;
T3 = 360000*3;
T4 = 360000*4;
CDt= 0.038;
CLt = 0.6;
rho = 1.275;
V1 = linspace(0,75,15);
VR = linspace(0,77,50);
W5 = 575000*9.81;
T0 = 0;
mub = 0.40;
p = g*((T4/W5)-mu);
q = -((g*(CDt-mu*CLt)*rho*S)/(2*W5));
sv1 = 1/(2*q)*(log(p+q*V1.^2)-log(p)); % distance to accelerate to v1
Vb = linspace(0,75,15);
pb = g*((T0/W5)-mub);
qb = -((g*(CDt-mub*CLt)*rho*S)/(2*W5));
svb = (1/(2*qb)*(log(pb)-log(pb+qb*Vb.^2))); % distance to decelerate from v1
p34 = g*((T3/W5)-mu);
q34 = -((g*(CDt-mu*CLt)*rho*S)/(2*W5));
Vlof = 90-V1;
sv34 = 1/(2*q34).*(log(p34+q34*Vlof.^2)-log(p34));
Z = [svb;sv34];
%plot the bar graph
b = bar(sv1);
hold on
%% Get the x coordinates of the tip of the bars
%y values can be obtained similarly, but they are simpy the values of the
%variable for which the bar is plotted
x = b.XEndPoints;
%% Calculating the half width of a bar
%i.e. = total available space * bar width of the graph / 2
%0.8 is the default bar width
hwidth = (x(2)-x(1))*0.8/2;
for k=1:numel(x)
patch(x(k) + [0 hwidth hwidth 0], sv1(k) + [0 0 Z(1,k) Z(1, k)], 'r', 'LineStyle', 'none')
patch(x(k) - [0 hwidth hwidth 0], sv1(k) + [0 0 Z(2,k) Z(2, k)], 'g', 'LineStyle', 'none')
end
%Bring the bar graph on top
uistack(b, 'top')
More Answers (1)
Adam Danz
on 20 Dec 2023
Is this what you're looking for? I don't know what "distance V1" is.
The only change I made was to add "stacked" to this line
bar(z_trans,'stacked');
S = 845;
mu = 0.015;
g = 9.81;
T1 = 360000;
T3 = 360000*3;
T4 = 360000*4;
CDt= 0.038;
CLt = 0.6;
rho = 1.275;
V1 = linspace(0,75,15);
VR = linspace(0,77,50);
W5 = 575000*9.81;
T0 = 0;
mub = 0.40;
p = g*((T4/W5)-mu);
q = -((g*(CDt-mu*CLt)*rho*S)/(2*W5));
sv1 = 1/(2*q)*(log(p+q*V1.^2)-log(p)); % distance to accelerate to v1
Vb = linspace(0,75,15);
pb = g*((T0/W5)-mub);
qb = -((g*(CDt-mub*CLt)*rho*S)/(2*W5));
svb = (1/(2*qb)*(log(pb)-log(pb+qb*Vb.^2))); % distance to decelerate from v1
p34 = g*((T3/W5)-mu);
q34 = -((g*(CDt-mu*CLt)*rho*S)/(2*W5));
Vlof = 90-V1;
sv34 = 1/(2*q34).*(log(p34+q34*Vlof.^2)-log(p34));
Z = [svb;sv34];
z_trans = transpose(Z);
bar(z_trans,'stacked'); % distance to V1
grid on
0 Comments
See Also
Categories
Find more on Line Plots 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!