Bar plot color, why [1 0 0] does not gives red

3 views (last 30 days)
Hi All and thanks for reading this.
I want to color some of the bars in this plot in red but the color I get is not as expected (as for rgb = [1 0 0] )
Am I doing something wrong? or ....
My code is below
figure('Color','w', 'Units','normalized', 'Position',[0.3906 0.6778 0.2037 0.2514])
yDataGreen = [ 5 7 9];
yDataRed = [ 6 8 10];
xData = 1:3;
barPlot1 = bar(xData, [yDataGreen; yDataRed]) ; hold on; grid minor; box on; title('That does not look the Red I want')
barPlot1(1).FaceColor = 'flat';
barPlot1(1).CData(:,:) = [0 1 0; 0 1 0; 0 1 0]; %green
barPlot1(2).CData(:,:) = [1 0 0; 1 0 0; 1 0 0]; %red

Accepted Answer

Voss
Voss on 15 Jul 2022
Edited: Voss on 15 Jul 2022
You have to set FaceColor to 'flat' in order for CData to take effect, which you are doing for the first bar but not for the second.
figure('Color','w', 'Units','normalized', 'Position',[0.3906 0.6778 0.2037 0.2514])
yDataGreen = [ 5 7 9];
yDataRed = [ 6 8 10];
xData = 1:3;
barPlot1 = bar(xData, [yDataGreen; yDataRed]) ; hold on; grid minor; box on; title('Now that does look like the Red you want')
barPlot1(1).FaceColor = 'flat';
barPlot1(2).FaceColor = 'flat'; % set both
% or set both at once:
% set(barPlot1,'FaceColor','flat')
barPlot1(1).CData(:,:) = [0 1 0; 0 1 0; 0 1 0]; %green
barPlot1(2).CData(:,:) = [1 0 0; 1 0 0; 1 0 0]; %red
  2 Comments
Voss
Voss on 15 Jul 2022
You're welcome! Any questions, let me know. Otherwise, please "Accept This Answer". Thanks!

Sign in to comment.

More Answers (0)

Categories

Find more on Discrete Data Plots in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!