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!