Drawing bar graph with different colors in Matlab 2013a
1 回表示 (過去 30 日間)
古いコメントを表示
I am working with Matlab 2013a and I want to make a bar graph of a vector like v2=[2.4 9 18]
I want each bar will have different color
I have tried the following code, but it does not work:
v2 = [2.4 9 18];
handle = bar(v2);
barColorMap = jet(3);
for k = 1:3
set(handle(k), 'FaceColor', barColorMap(k, :));
end
but it does not work. I appreciate if someone can help me on this.
0 件のコメント
回答 (1 件)
ag
2025 年 5 月 6 日
Hi Shirin,
To define the color for a "Bar" individually, you can use the bar property "CData". You will first need to set the "FaceColor" property of the bar object to "flat" so that the chart uses the colors defined in the "CData" property.
The below code demonstrates how to achieve the same:
v2 = [2.4 9 18];
handle = bar(v2);
barColorMap = jet(3);
handle.FaceColor = 'flat';
handle.CData = barColorMap;
For more details, please refer to the following MathWorks documentation: https://www.mathworks.com/help/matlab/ref/bar.html#d126e83000:~:text=LineWidth%27%2C1.5)-,Control%20Individual%20Bar%20Colors,-Copy%20Code
Hope this helps!
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Color and Styling についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!