How can I stack two bars next to each other on top of a single bar in a bar graph?

11 次查看(过去 30 天)
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
  1 个评论
Dyuman Joshi
Dyuman Joshi 2023-12-20
One approach for obtaining the output would be to use patch/fill objects on top of the bar plot.
However, it's too late here, so I am off. Try experimenting with the approach. Hopefully someone else can help you in case you have more questions regarding the approach or your question in general.

请先登录,再进行评论。

采纳的回答

Dyuman Joshi
Dyuman Joshi 2023-12-22
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')

更多回答(1 个)

Adam Danz
Adam Danz 2023-12-20
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

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by