Set legend color in stacked bar plot

3 ビュー (過去 30 日間)
Matlab2010
Matlab2010 2013 年 11 月 15 日
コメント済み: Tong Zhao 2018 年 5 月 20 日
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);

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 11 月 15 日
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);
  1 件のコメント
Tong Zhao
Tong Zhao 2018 年 5 月 20 日
You always rock!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by