# Histogram function plots different colours from those requested

144 visualizzazioni (ultimi 30 giorni)
z8080 il 28 Mar 2022
Commentato: z8080 il 29 Mar 2022
I"m trying to color-code each of 4 histograms using a predefined color scheme. Here's a minimal working example:
colours = [0 32 96;
192 0 0;
71 207 255;
255 143 143;
] / 255;
for i=1:4
x = randn(1,100);
subplot(1,4, i)
values = histogram(x, 'FaceColor', colours (i, :));
end
However, in the image I'm getting, the colors are actually (slightly) different, for instance for the first histogram I get (102,121,160) instead of (0,32,96):
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Steven Lord il 28 Mar 2022
The histogram object has a FaceAlpha property, so that if you overlay two histogram objects you can see the one underneath. If you change that FaceAlpha property the color appears slightly different and may be closer to what you want.
x = randn(1, 100);
figure
h = histogram(x, 'FaceColor', 'r');
title("Histograph with FaceAlpha " + h.FaceAlpha)
figure
h = histogram(x, 'FaceColor', 'r', 'FaceAlpha', 1);
title("Histograph with FaceAlpha " + h.FaceAlpha)
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
z8080 il 29 Mar 2022
Yup, that was it :) Thanks Steven!

Accedi per commentare.

### Più risposte (1)

Scott MacKenzie il 28 Mar 2022
Modificato: Scott MacKenzie il 28 Mar 2022
The histogram function uses a face alpha of 0.6 by default. That's why the colors appear a bit lighter than the values from your eye dropper app. If you set the face alpha to 1.0 (see below), then the displayed colors will agree with the eye dropper values.
colours = [0 32 96;
192 0 0;
71 207 255;
255 143 143;
] / 255;
for i=1:4
x = randn(1,100);
subplot(2,4, i)
values = histogram(x,'FaceColor', colours(i, :));
values.FaceAlpha = 1.0; % NOTE: default is 0.6
values.FaceColor * 255
end
ans = 1×3
0 32 96
ans = 1×3
192 0 0
ans = 1×3
71 207 255
ans = 1×3
255 143 143
##### 3 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
Scott MacKenzie il 28 Mar 2022
Modificato: Scott MacKenzie il 28 Mar 2022
@z8080, hmm, right your are. I dug a bit deeper and discovered why there is a discrepancy between the set colors and the eye dropper colors. See my modified answer for the details.
Seems as I was modifying my answer, @Steven Lord posted an answer. Same story.
z8080 il 29 Mar 2022
Indeed, but thanks again Scott!

Accedi per commentare.

### Categorie

Scopri di più su Histograms in Help Center e File Exchange

R2019b

### Community Treasure Hunt

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

Start Hunting!

Translated by