MATLAB: How to assign individual colors to grouped and stacked elements in bar chart

grouped barindividual colorsMATLABstacked bar

Hi all!
%Öffnen des benötigten workspace:
load('sensitivity_analysis.mat');
Data = zeros(9,3,3);
Data(:,:,1) = production_wo_elec;
Data(:,:,2) = production_variable;
Data(:,:,3) = usephase;
groupLabels = { 'hc small', 'hc medium', 'hc large', 'mix small', 'mix medium', 'mix large','green small','green medium','green large'}; % set labels
plotBarStackGroups(Data, groupLabels); % plot groups of stacked bars
function [] = plotBarStackGroups(Data, 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(Data, 1);
NumStacksPerGroup = size(Data, 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(Data(:,i,:));
% Center the bars:
internalPosCount = i - ((NumStacksPerGroup+1) / 2);
% Offset the group draw positions:
groupDrawPos = (internalPosCount)* groupOffset + groupBins;
h(i,:) = bar(Y, 'stacked','Facecolor','flat');
set(h(i,:),'BarWidth',groupOffset);
set(h(i,:),'XData',groupDrawPos);
if i==1
h(1,1).CData=[189/255 183/255 107/255];
h(1,2).CData=[ 77/255 77/255 1];
h(1,3).CData=[173/255 1 47/255];
elseif i==2
h(2,1).CData=[189/255 183/255 107/255];
h(2,2).CData=[ 77/255 77/255 1];
elseif i==3
h(3,1).CData=[189/255 183/255 107/255];
h(3,2).CData=[ 77/255 77/255 1];
h(3,3).CData=[0 0 0];
end
end
%h=gca;
%h.FaceColor='flat';
%h.CData(2,:)=[.5 0 .5];
hold off;
set(gca,'XTickMode','manual');
set(gca,'XTick',1:NumGroupsPerAxis);
set(gca,'XTickLabelMode','manual');
set(gca,'XTickLabel',groupLabels);
end
What I want to do is: in the first three Groups the middle Elements should be black instead of blue (as they represent small/medium/large amounts of electricity generated with hard coal) while in the groups 7 to 9 the middle Elements should be green instead of blue cause they represent electricity from renewable energy.
Cheers, Sonja

Best Answer

Note that I have changed the solution completely after the discussion below. You may now change the color of any single bar-segments. This solution is also independent of third-party functions. Load the data (given in the comments) and ticklabels before running.
%Fix data structure
Data=permute(Data,[2 1 3])
Data=reshape(Data,27,3)
%set some input
Groups=9;
Stacks=3;
NumInGroup=length(Data)/Groups;
NumBars=Groups*NumInGroup;
%Plot
figure;hold on;
for i=1:Groups*Stacks;
h(i,1:3)=bar([Data(i,:);nan(1,3)],'stacked');
end
%Group and set xdata
x1=1:Groups
x0=x1-0.25;
x2=x1+0.25;
pos=[x0;x1;x2];
xpos=pos(:)';
for i=1:Stacks*Groups
set(h(i,:),'xdata',xpos(i))
end
set(h,'barwidth',0.25)
%Set labels
set(gca,'xtick',[1:Groups],...
'xticklabels',groupLabels)
%Set colors
set(h(:,1),'facecolor',[.7 .7 .7])
set(h(1:9,2),'facecolor',[0 0 1])
set(h(10:end-9,2),'facecolor',[0.7 0.3 0.4])
set(h(end-8:end,2),'facecolor',[0 1 0])
set(h(1:3:end,3),'facecolor',[1 1 1])
set(h(2:3:end,3),'facecolor',[0.5 0.1 0.9])
set(h(3:3:end,3),'facecolor',[0 0 0])