
How can I change the colour of specific bars in a bar graph
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I want to change the colour of the bars in a bar graph according to a category that each of the variables fall into.
For example, there are 25 variables (25 bars on my bar graph). Bars 1 -7 are a category, 8 - 10, 11- 9 a, 20-22, and 23-25.
Here are the values for the y-axis:
0.3593, -1.2546, 0.9986, 4.1348, -2.3126, -0.2999, -2.8599; (genetics category)
6.7126, -1.9890, -1.9099; (biomarkers category)
8.8851, 8.5652, 6.2261, 4.5569, 7.0883, 9.5378, 7.5932, 14.9368, 15.7487; (cognitive category)
-9.7093, -6.9533, -5.8677; (psychological category)
-1.6640, -1.1083, -0.9188; (vascular cateogory)
*Attached is what the current graph looks like in one colour
0 个评论
采纳的回答
Doddy Kastanya
2020-12-17
You can create a handle on the graph and replace the properties accordingly. Here is an example.
>> x=[1:10];
>> b=bar(x);
>> b.FaceColor='flat';
>> b.CData(2,:)=[0.5 0 0.5];
>> b.CData(6,:)=[0.3 0.2 0.5];
>> b.CData(10,:)=[0.1 0.7 0.2];
The last three lines changes the color of the second, sixth, and last bar.

I hope this helps.
3 个评论
Doddy Kastanya
2020-12-17
You can simply use a for-loop for this. For example:
>> for i=1:4,
b.CData(i,:)=[0.5 0 0.5];
end

更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graph and Network Algorithms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!