How to achieve stacked bar graph with two 'y' values but same x axi

Hello!

I am trying to show the lower value which is hidden at he back while plotting a stacked bar graph to come in front.

I am reading values from excel sheet

arrival_rate = xlsread('Graph_data', 'Sheet2', 'H1:H6');
pe_a = xlsread('Graph_data', 'Sheet2', 'I1:I6');
pe_b = xlsread('Graph_data', 'Sheet2', 'J1:J6');
line = xlsread('Graph_data', 'Sheet2', 'K1:K6');
   x1 = 30:1:100;
   x2 = 30:1:100;
   y2 = interp1(arrival_rate,line,x2,'pchip') ;
hold on 
bar(arrival_rate,pe_a, .1 , 'stacked','DisplayName', 'Error in A') 
bar(arrival_rate,pe_b, .1 , 'stacked', 'DisplayName', 'Error in B ')
plot(x2,y2,'k.','HandleVisibility','off','LineWidth',1) 
plot(arrival_rate,line,'k*', 'HandleVisibility','off','LineWidth',1) 
hold off
   xlabel('\lambda (Clients/Hour) ')
   ylabel('Error (%)')
   legend show
   legend('Location','Northeast')
   set(gca,'XTick',(0:10:110))
   set(gca,'YTick',(0:5:50))
    set(gcf, 'PaperUnits', 'normalized')
    set(gcf, 'PaperOrientation', 'landscape') 
    set(gcf, 'PaperPosition', [0 0 1 1]) 

the above code is what I wrote and it gives me an image attached below.

Is there a way to bring the blue bar forward as it has the lower value for the case of x =80 and 100.

답변 (1개)

TED MOSBY
TED MOSBY 2024년 11월 13일
편집: TED MOSBY 2024년 11월 18일
You just need to change order of bar plotting according to the values as I have shown in the modified part from your code below:
% Interpolation for the line plot
x2 = 30:1:100;
y2 = interp1(arrival_rate, line, x2, 'pchip');
% Hold on for multiple plots
hold on
% Plot bars conditionally (smaller one in front)
for i = 1:length(arrival_rate)
if pe_a(i) < pe_b(i)
% Plot smaller bar first (pe_a), larger bar second (pe_b)
bar(arrival_rate(i), pe_b(i), 1, 'FaceColor', 'b');
bar(arrival_rate(i), pe_a(i), 1, 'FaceColor', 'r');
else
% Plot smaller bar first (pe_b), larger bar second (pe_a)
bar(arrival_rate(i), pe_a(i), 1, 'FaceColor', 'r');
bar(arrival_rate(i), pe_b(i), 1, 'FaceColor', 'b');
end
end
% Plot other lines
plot(x2, y2, 'k.', 'HandleVisibility', 'off', 'LineWidth', 1);
plot(arrival_rate, line, 'k*', 'HandleVisibility', 'off', 'LineWidth', 1);
% Finish the plot
hold off
% Set the legend only once outside the loop
legend({'Error in B', 'Error in A'}, 'Location', 'Northeast');
xlabel('\lambda (Clients/Hour) ')
ylabel('Error (%)')
Hope this helps!

카테고리

도움말 센터File Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

질문:

2018년 4월 25일

편집:

2024년 11월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by