grouped stacked bar with specific colours
27 ビュー (過去 30 日間)
古いコメントを表示
I am using the function in the link below to plot grouped stacked bars.
I have slightly modified it to add the legend to the plot. I get the following histogram:

I would like to change the colours of the bars with specific colours (for example: dark blue, blue and light blue for the first stacked bars, dark green, green and light green for the second stacked bars, and dark orange, orange and light orange for the third stacked bars). I have tried with adding the following lines inside the loop, but it did not work:
if i == 1
col = ['[0.8 0 0.8]','[0 0.6 0]','[0.9 0.5 0 ]'];
elseif i == 2
col = ['[1 0 1]','[0 0.8 0 ]','[1 0.6 0]'];
else
col = ['[1 0.2 1]','[0 1 0]','[1 0.9 0]'];
end
set(h(i,:),'Color',col);
2 件のコメント
回答 (1 件)
Adam Danz
2019 年 9 月 23 日
編集済み: Adam Danz
2019 年 9 月 25 日
Within the plotBarStackGroups() function, bar handles are stored in 'h'. Add that as an output to the function so you have access to the bar handles (this output should have been included in the first place, it currently has no outputs).
function h = plotBarStackGroups(stackData, groupLabels)
'h' is an n-by-m array of bar handles with n bars per group and m segments per bar.
To change the color of each segment within each bar,
% Create a stacked, grouped bar chart using the function
stackData = randi(100,3,5,7);
groupLabels = {'a' 'b' 'c'};
h = plotBarStackGroups(stackData, groupLabels)
% Chance the colors of each bar segment
colors = jet(size(h,2)); %or define your own color order; 1 for each m segments
colors = repelem(colors,size(h,1),1);
colors = mat2cell(colors,ones(size(colors,1),1),3);
set(h,{'FaceColor'},colors)
2 件のコメント
Adam Danz
2021 年 6 月 10 日
編集済み: Adam Danz
2021 年 6 月 10 日
You could use a colorbar: https://www.mathworks.com/matlabcentral/answers/833923-how-to-re-scale-the-colorbar-without-affecting-the-graph#comment_1534028
or you could use the legend function along with the bar handles: https://www.mathworks.com/matlabcentral/answers/836793-plot-stacked-bar-chart-with-legends#answer_705908
参考
カテゴリ
Help Center および File Exchange で Data Distribution Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

