How do I get the right colors in histogram?
8 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Steven Lord
2016-6-17
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Histograms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!