Code covered by the BSD License

### Highlights fromPlot and compare histograms; pretty by default

4.84615

4.8 | 14 ratings Rate this file 87 Downloads (last 30 days) File Size: 17.9 KB File ID: #27388

# Plot and compare histograms; pretty by default

28 Apr 2010 (Updated 20 Mar 2013)

Compares multiple sets of data elegantly. Set bins and axis bounds to be appropriate for the data.

File Information
Description

Summary of what function does:
1) Automatically sets the number and range of the bins to be appropriate for the data.
2) Compares multiple sets of data elegantly on one or more plots, with legend or titles. It also graphs the mean and standard deviations. It can also plot the median and mode.
3) Outputs text with the useful statistics for each distribution.
4) Allows for changing many more parameters

Highlighted features (see below for details)
'separate' to plot each set on its own axis, but with the same bounds
'binfactor' change the number of bins used, larger value =more bins
'samebins' force all bins to be the same for all plots
'legend' add a legend in the graph (default for structs)
'noerror' remove the mean and std plot from the graph
'median' add the median of the data to the graph
'text' return many details about each graph even if not plotted

Syntax:
t = nhist(Y) bins the elements of Y into equally spaced containers
and returns a string with information about the distributions.
If Y is a cell array or a structure nhist will make graph the
binned (discrete) probability density function of each data
set for comparison on the same graph. It will return A cell
array or structure which includes a string for each set of
data.

[t, N, X]= nhist(...) also returns the number of items in each bin, N,
and the locations of the left edges of each bin. If Y is a
cell array or structure then the output is in the same form.

nhist(Y,'PropertyName', . . . )
nhist(Y,'PropertyName',PropertyValue, . . . )

% Examples
Cell array example:
A={randn(1,10^5),randn(10^3,1)+1};
nhist(A,'legend',{'u=0','u=1'});
nhist(A,'legend',{'u=0','u=1'},'separate');

Structure example:
A.zero=randn(1,10^5); A.one=randn(10^3,1)+1;
nhist(A);
nhist(A,'color','summer')
nhist(A,'color',[.3 .8 .3],'separate','median','noerror')
nhist(A,'binfactor',4)
nhist(A,'samebins')
nhist(A,'median','noerror')

Acknowledgements

This file inspired Tactics Toolbox.

MATLAB release MATLAB 7.8 (R2009a)
Tags for This File
Everyone's Tags
Tags I've Applied
19 Jun 2013

It would be nice in the future to have the option to provide custom colors as an argument for 'color'. For instance, if plotting data d={setA,setB}, I'd like to be able to specify line colors besides the standard colormaps for setA and setB. So for example:
nhist(d,... 'color',{[1 0 0],[0 1 1]});
Or equivalently:
nhist(d,... 'color',{'r','c'}).

14 May 2013
14 May 2013
20 Mar 2013

@Duane, yes it does its best with all kinds of data. I just changed the title of the file to hopefully be more clear.

20 Mar 2013

You say "Plot and compare nice histograms by default." Does this function also work for histograms that are not nice?

28 Feb 2013

I am using this function is my app. Very usful. Thank you.

05 Nov 2012
16 Jul 2012

Very useful plotting function.

I do have a suggestion though: add an option to do the 'separate' plots option in a 2d grid, instead of having only a single column of plots.

09 Jul 2012

@Shinobue111, a PDF may be >1 as long as the area = 1, as we discussed.

26 Jun 2012

I love this function. However, I'm getting PDF values of >1 when I compare several distributions on one figure... that shouldn't really be possible :/

08 Feb 2012

very flexible by using the provided parameters. useful to draw concise but informative histgrams. Thanks for the nice work.

28 Nov 2011

Thanks for the suggestion - I went ahead and implemented robust handling of NaN and inf data points.

23 Nov 2011

I really like this function, but I wish it could handle NaNs in my data. Thanks for sharing your code.

15 Sep 2011

Very nice bit of code and very useful. Thanks for posting it.

09 Jun 2011

Well documented and many features, great job!

25 Oct 2010

Great script, Jonathan, just what I was looking for! Best regards

17 Aug 2010
16 Jun 2010

Great. Works cleanly and comprehensively. It also runs where, for me, hist crashes (http://www.mathworks.de/matlabcentral/newsreader/view_thread/240876).

03 May 2010

For many data sets it may be more useful to have a standard histogram and overlay a smooth fit. You should at least provide an option for creating a smooth fit.

Please don't rate your own files: you, of course, think your work is amazing. That's why you've put it up here for all to see.

29 Apr 2010

I forgot to include the linspecer function, sorry!

13 May 2010

Added an option to plot a smooth fit instead of the default stairs function (Rob' suggestion).

21 May 2010

implemented ''npoints' for passing in structures.

04 Jun 2010

Expanded the function to allow data points which are infinite (in value, not in number!)

24 Nov 2011

I have implemented robust handling of NaN and inf data points. Changed underscores in struct field names "_" to spaces in the titles and legends.

19 Mar 2013

I improved the aesthetic choice of axis bounds in cases when some of your plots have no data.

20 Mar 2013

changed the name of the function to be more clear