Set legend color in stacked bar plot

3 visualizaciones (últimos 30 días)
Matlab2010
Matlab2010 el 15 de Nov. de 2013
Comentada: Tong Zhao el 20 de Mayo de 2018
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);

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 15 de Nov. de 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);

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by