How do I get the right colors in histogram?

4 views (last 30 days)
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

Accepted Answer

Steven Lord
Steven Lord on 17 Jun 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 Comment
pedro
pedro on 17 Jun 2016
Edited: pedro on 17 Jun 2016
Steve,
Worked as advertised, thanx!
pedro

Sign in to comment.

More Answers (0)

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!