Azzera filtri
Azzera filtri

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

5 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Voss
Voss il 15 Lug 2022
Modificato: Voss il 15 Lug 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 Commenti
Voss
Voss il 15 Lug 2022
You're welcome! Any questions, let me know. Otherwise, please "Accept This Answer". Thanks!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Discrete Data Plots in Help Center e File Exchange

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by