Set legend color in stacked bar plot

3 views (last 30 days)
I wish to use a stacked bar graph and specify my own colors. Have do I make the colors in the legend match the bar? thanks
data2D = rand(10,6);
H=bar(data2D, 'stack');
P=findobj(gca,'type','patch');
myC= [0 0 1
1 0 0
1 0.4 0
0 0.8 1
0.6 0 1
0 1 0 ];
for n= 1 : length(P)
set(P(n),'facecolor',myC(n,:));
end
AX=legend(H, {'a','b','c','d','e','f'}, 'Location','Best');
LEG = findobj(AX,'type','text');
set(LEG,'FontSize',8);

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 15 Nov 2013
close
myC= [0 0 1
1 0 0
1 0.4 0
0 0.8 1
0.6 0 1
0 1 0 ];
data2D = rand(10,6);
H=bar(data2D, 'stack');
for k=1:6
set(H(k),'facecolor',myC(k,:))
end
AX=legend(H, {'a','b','c','d','e','f'}, 'Location','Best','FontSize',8);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!