Effacer les filtres
Effacer les filtres

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

7 vues (au cours des 30 derniers jours)
Giuseppe Naselli
Giuseppe Naselli le 15 Juil 2022
Commenté : Voss le 15 Juil 2022
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

Réponse acceptée

Voss
Voss le 15 Juil 2022
Modifié(e) : Voss le 15 Juil 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 commentaires
Giuseppe Naselli
Giuseppe Naselli le 15 Juil 2022
Thanks!!!!!!
Voss
Voss le 15 Juil 2022
You're welcome! Any questions, let me know. Otherwise, please "Accept This Answer". Thanks!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Discrete Data Plots dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by