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

7 次查看(过去 30 天)
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

采纳的回答

Voss
Voss 2022-7-15
编辑:Voss 2022-7-15
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Discrete Data Plots 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by