如何使条形图中的较低值可见?

How to make the lower value in bar graph visible?

我正在尝试从电子表格中读取值并通过 MATLAB 绘制它们。但是,当第一个数据集的值变低时,它会隐藏在后面,如下面的值 80100 所示:

我想可能是因为我在同一点上绘制了两个不同的 bar 图。我怎样才能把较短的酒吧放在前面?

下面是我的代码:

%{
Data example:    

Arrival_rate   per_A        per_B

30             46.361       44.892
60             30.585       25.602
80             20.674       23.352
100            16.209       18.597
%}

fig = figure('DefaultAxesFontSize',18);
set(0,'DefaultAxesFontName','Times New Roman');

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;
y1 = [46.361 44.892; 30.585 25.602; 16.209  18.597];
x2 = 30:1:100;
y2 = interp1(arrival_rate,line,x2,'pchip') ;

hold on 

ylabel('% error in VM_A')

% bar(arrival_rate,y,  0.2, 'b', 'DisplayName', 'Error in A') 
bar(arrival_rate,pe_a ,.1 , 'stacked','DisplayName', 'Error in A') 
bar(arrival_rate,pe_b, .1 , 'stacked', 'DisplayName', 'Error in B')

% bar(arrival_rate, y1, 0.2, 'hist')

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 (%)')
%  title('Effect of Probability of a VM Type on awt of Clients')
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])  

据我了解,对于蓝条比红条短的情况,应该出现在前面。不幸的是,MATLAB 并没有让您轻松地单独控制每个条形,这应该暗示这不是他们打算做的事情。 (换句话说 - 考虑以有意义的方式可视化数据的其他方式是个好主意。)

我建议你让条形的宽度稍微不同,这样你就可以很容易地一眼就看出发生了什么:

D = [...
30             46.361       44.892
60             30.585       25.602
80             20.674       23.352
100            16.209       18.597];

arrival_rate = D(:,1);
pe_a = D(:,2);
pe_b = D(:,3);

fig = figure('DefaultAxesFontSize',18);
bar(arrival_rate,pe_a ,.15 , 'stacked','DisplayName', 'Error in A'); hold on;
bar(arrival_rate,pe_b, .1 , 'stacked', 'DisplayName', 'Error in B');

给出:

现在,如果您坚持按自己的方式行事,我可以想到一个解决方案,但扩展到 2 个以上的组会越来越困难。简而言之,您按此顺序绘图,然后它们就会正确显示:

  1. 蓝色(可选:仅在蓝色较高的地方)。
  2. 红色。
  3. 蓝色超过红色(当红色更高时)。

fig = figure('DefaultAxesFontSize',18);

bar(arrival_rate,pe_a, .1 , 'DisplayName', 'Error in A'); hold on;
bar(arrival_rate,pe_b, .1 , 'DisplayName', 'Error in B');
bar(arrival_rate(is_blue_smaller), pe_a(is_blue_smaller), .1 , ...
  'FaceColor', lines(1), 'HandleVisibility','off');

产生: