How do I get the right colors in histogram?
조회 수: 4 (최근 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!