How do I get the right colors in histogram?

4 ビュー (過去 30 日間)
pedro
pedro 2016 年 6 月 17 日
編集済み: pedro 2016 年 6 月 17 日
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

採用された回答

Steven Lord
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
  1 件のコメント
pedro
pedro 2016 年 6 月 17 日
編集済み: pedro 2016 年 6 月 17 日
Steve,
Worked as advertised, thanx!
pedro

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by