Can HIST take parameters such as FaceColor and EdgeColor?

14 views (last 30 days)
HIST only allows me to input data and number of bins. I would also like to be able to specify the color of the histogram and the line colors of the histogram. Currently, I have to create my histogram and then use FINDOBJ to get the handles to the patch objects before setting their color.
For example:
x = -2.9:0.1:2.9;
y = randn(10000,1);
hist(y,x)
h = findobj(gca,'Type','patch');
set(h,'FaceColor','r','EdgeColor','w')
This becomes more problematic when I have multiple histogram plots in one axes and I want each to be its own color.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
As a workaround, you can use the following example code as a guide:
% Create some sample data
x = -2.9:0.1:2.9;
y = randn(10000,1);
% Exploit the use of HIST where we return data instead of a plot
[n1,x1]=hist(y,x);
[n2,x2]=hist(y+6,x+6);
% Use the output of HIST to create BAR plots
% Note that this exploits the undocumented 'hist' option, used solely by HIST
% It is not recommended that you rely on this behavior, but rather you use it as a workaround
% Return handles on these calls so we can manipulate them later
h1=bar(x1,n1,'hist');
hold on
h2=bar(x2,n2,'hist');
% Now just modify the color of one of the BAR plots
set(h2,'facecolor','red')

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!