Borrar filtros
Borrar filtros

How do I get the right colors in histogram?

4 visualizaciones (últimos 30 días)
pedro
pedro el 17 de Jun. de 2016
Editada: pedro el 17 de Jun. de 2016
Greetings! I'm having trouble setting histogram bar colors in matlab after 2014a. The following code (using bar), works fine both before and after, meaning that the bars are (bright) red, as expected:
data1=randi(9,4,1); bh=bar(data1) set(bh,'FaceColor',[1,0,0])
The following code (using hist) also produces the desired red bars before (with 2014a):
data2=randi(9,99,1);
hist(data2)
h = findobj(gca,'Type','patch');
set(h(1), 'FaceColor',[1,0,0])
However, in 2015a (the latest I have access to) the following code (using histogram) produces pink bars, not red:
h1=histogram(data2);
h1.FaceColor=[1,0,0];
What am I doing wrong?
I'm still trying to wrap my mind around the new graphics, so any help would be appreciated.
Cheers, pedro

Respuesta aceptada

Steven Lord
Steven Lord el 17 de Jun. de 2016
By default, the histogram plot is partially transparent. [That way if you have two of them on the same axes, you can see both of them.] Its FaceAlpha property defaults to 0.6. That's what makes it look more "pink" than red. Change FaceAlpha to 1 to make it opaque, 0 to make it completely transparent.
data2=randi(9,99,1);
h1=histogram(data2);
h1.FaceColor=[1,0,0];
ax = ancestor(h1, 'axes'); % Get the axes handle so I can update the title
for k = 0:100
h1.FaceAlpha = k/100;
title(ax, sprintf('FaceAlpha is %d/100', k)); % Show the current FaceAlpha value
pause(0.1)
end
  1 comentario
pedro
pedro el 17 de Jun. de 2016
Editada: pedro el 17 de Jun. de 2016
Steve,
Worked as advertised, thanx!
pedro

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Histograms en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by