画堆叠直方图的打包函数
function [] = plotBarStackGroups(stackData, groupLabels)
%% Plot a set of stacked bars, but group them according to labels provided.
%%
%% Params:
%% stackData is a 3D matrix (i.e., stackData(i, j, k) => (Group, Stack, StackElement))
%% groupLabels is a CELL type (i.e., { 'a', 1 , 20, 'because' };)
%%
%% Copyright 2011 Evan Bollig (bollig at scs DOT fsu ANOTHERDOT edu
%%
%%
NumGroupsPerAxis = size(stackData, 1);
NumStacksPerGroup = size(stackData, 2);
% Count off the number of bins
groupBins = 1:NumGroupsPerAxis;
MaxGroupWidth = 0.65; % Fraction of 1. If 1, then we have all bars in groups touching
groupOffset = MaxGroupWidth/NumStacksPerGroup;
figure
hold on;
for i=1:NumStacksPerGroup
Y = squeeze(stackData(:,i,:));
% Center the bars:
internalPosCount = i - ((NumStacksPerGroup+1) / 2);
% Offset the group draw positions:
groupDrawPos = (internalPosCount)* groupOffset + groupBins;
h(i,:) = bar(Y, 'stacked');
set(h(i,:),'BarWidth',groupOffset);
set(h(i,:),'XData',groupDrawPos);
end
hold off;
set(gca,'XTickMode','manual');
set(gca,'XTick',1:NumGroupsPerAxis);
set(gca,'XTickLabelMode','manual');
set(gca,'XTickLabel',groupLabels);
end
调用函数
A =[
3462 2077 692 692
409 245 82 82
2417 1450 483 483
8554 5132 1711 1711
1620 972 324 324
54 32 11 11
5000 3000 1000 1000
609 365 122 122
693 416 139 139];
A=A(:,2:4);
A=reshape(A,[9 1 3]);
x={'Center','Donut','Edge-local','Edge-ring','Local','Near-full','None','Random','Scratch'}
plotBarStackGroups(A,x);
结果如图