Explicit color and legend control bars in stacked bar plot

4 ビュー (過去 30 日間)
Bibigul Shektybayeva
Bibigul Shektybayeva 2017 年 9 月 21 日
コメント済み: Alberto Mora 2018 年 5 月 25 日
I would like to create a bar plot with each bar of different color, so for the example below I want to have 9 different colors, instead of each column of the matrix having a different number. Is it possible? I also would like to have the same color for the same entry in a different bar plot, such that I can see how the bars are redistributed. So, I want to have the bars corresponding to number 8 have the same color in both graphs. Could anyone suggest how to do that? a = [1,3,6;2,5,6;8,2,1] H=bar(a, 'stacked'); [a, ind]=sort(a(:)) a=reshape(a, 3, 3) figure H=bar(a, 'stacked');

回答 (1 件)

Niraj Gadakari
Niraj Gadakari 2017 年 9 月 28 日
編集済み: Niraj Gadakari 2017 年 9 月 28 日
You can use the new feature, Color Data (CData) property for bar graph released in R2017b. You can change the color for a particular bar by setting the FaceColor property to 'flat'. Then change the corresponding row in the CData matrix to the new RGB triplet. Here is the link for the CData property in bar graph: CData
For example,
a = [1,3,6;2,5,6;8,2,1];
H=bar(a, 'stacked');
colorSet = [];
for i = 1:3
myColors = round(rand(3,3),1);
colorSet = [colorSet myColors];
H(i).FaceColor = 'flat';
H(i).CData = myColors;
end
This will display the bar plot with different colors. Similarly for the different bar plot you can set the CData property of the other bar graph to the above colors.
  2 件のコメント
Bibigul Shektybayeva
Bibigul Shektybayeva 2017 年 9 月 29 日
Thank you for your answer! Is there a workaround for R2015b release?
Alberto Mora
Alberto Mora 2018 年 5 月 25 日
Same of my problem. You just declare in FaceColor field:
H(1).FaceColor = 'b';
H(2).FaceColor = 'r';
and so on...

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

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by