How can I change the colour of specific bars in a bar graph
1 view (last 30 days)
Show older comments
Alex Samson
on 17 Dec 2020
Commented: Alex Samson
on 18 Dec 2020
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 Comments
Accepted Answer
Doddy Kastanya
on 17 Dec 2020
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 Comments
Doddy Kastanya
on 17 Dec 2020
You can simply use a for-loop for this. For example:
>> for i=1:4,
b.CData(i,:)=[0.5 0 0.5];
end
More Answers (0)
See Also
Categories
Find more on Bar Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!