在matlab中绘制一个堆积条形图,显示所有值

时间:2013-04-27 13:16:06

标签: matlab bar-chart stackedbarseries

您好我在Matlab中有以下代码

CC_monthly_thermal_demand = [495 500 500 195 210 100 70 65 85 265 320 430]';
AD_monthly_thermal_generation_250 = [193 193 193 193 193 193 193 193 193 193 193 193]';
figure;
bar(1:12,AD_monthly_thermal_generation_250)
hold on
bar(1:12,CC_monthly_thermal_demand,'r')
set(gca,'XTickLabel',{'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',' Sep', 'Oct', 'Nov',' Dec'},'FontSize',18)
title('Seasonal Anaerobic Digestion (250kWe) Thermal Energy Supply to Demand - 2012','FontSize',22)
ylabel('Thermal Energy (MWhe)')
legend('250 kWth Supply','Thermal Energy Demand')
grid on
axis([0 13 0 600])

我正在尝试绘制一个堆积条形图,显示每个条形的每个变量的颜色。但是,对于“AD_monthly_thermal_generation_250”的值低于“CC_monthly_thermal_demand”的条形,“AD_monthly_thermal_generation_250”颜色完全被“CC_monthly_thermal_demand”覆盖,因此我看不到这些值。是否有可能看到它们?

谢谢

1 个答案:

答案 0 :(得分:0)

将输入组合在一起,使每个系列都是一列,然后使用选项'stack'

A = [495 500 500 195 210 100 70 65 85 265 320 430]';
B = [193 193 193 193 20  193 193 193 193 193 193 193]';
bar([B,A],'stacked')

编辑处理评论:

% Create random data and filler
A      = randi(100,10,1);
B      = randi(100,10,1);
mxA    = max(A);
filler = mxA-A;

% Plot stacked bar chart
h = bar([A,filler,B],'stacked');

% Set the filler to transparent
set(h(2),'Face','none','Edge','none')

% Set y-labels
yticks = linspace(0,max(B),5) + mxA;
set(gca,'Ytick', [linspace(0,mxA,5) yticks(2:end)],'YtickL', [linspace(0,mxA,5) yticks(2:end)-mxA])

enter image description here