Drawing bar graph with different colors in Matlab 2013a

1 回表示 (過去 30 日間)
shirin vafaei
shirin vafaei 2019 年 7 月 25 日
回答済み: ag 2025 年 5 月 6 日
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.

回答 (1 件)

ag
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;
Hope this helps!

カテゴリ

Help Center および File ExchangeColor and Styling についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by