Bar chart colors depending on their value

2 views (last 30 days)
(in MATLAB 2014a)
I would like to plot visuals to verify if a set of data complies with the requirements values.
req = [10,5,3];
data=[9,6,3];
To plot the following bar chart I used:
bar(req,0.5)
newXticklabel = {'label1','label2','label3'};
set(gca,'XtickLabel',newXticklabel);
hold on
bar(data,0.25,'g')
hold off
My question is: how do I change the color of one bar in red if the data is lower than the requirement value ?

Accepted Answer

Star Strider
Star Strider on 10 Dec 2018
Try this:
req = [10,5,3];
data=[9,6,3];
hb1 = bar(req,0.5);
newXticklabel = {'label1','label2','label3'};
set(gca,'XtickLabel',newXticklabel);
hold on
hb2 = bar(data,0.25,'g');
barcomp = hb2.YData < hb1.YData; % Logical Vector Identifying Bar
newbar = bar(hb2.XData(barcomp), hb2.YData(barcomp), hb2.BarWidth, 'r'); % Overplot In Red
hold off
  4 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Line 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!