4.85714

4.9 | 7 ratings Rate this file 87 Downloads (last 30 days) File Size: 17.79 KB File ID: #27388
image thumbnail

Plot and compare nice histograms by default

by Jonathan C. Lansey

 

28 Apr 2010 (Updated 24 Nov 2011)

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

| Watch this File

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')

MATLAB release MATLAB 7.8 (R2009a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (9)
03 May 2010 Rob Campbell

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.

16 Jun 2010 Simon Robinson

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

17 Aug 2010 Ahmed  
25 Oct 2010 Guido Lagos

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

09 Jun 2011 Laurent S

Well documented and many features, great job!

15 Sep 2011 Mark Brandon

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

23 Nov 2011 Amanda Whitmire

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

28 Nov 2011 Jonathan C. Lansey

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

08 Feb 2012 Jiaqi

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

Please login to add a comment or rating.
Updates
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.

Tag Activity for this File
Tag Applied By Date/Time
histogram Jonathan C. Lansey 28 Apr 2010 10:08:16
bins Jonathan C. Lansey 28 Apr 2010 10:08:16
bin size Jonathan C. Lansey 28 Apr 2010 10:08:16
axis bounds Jonathan C. Lansey 28 Apr 2010 10:08:16
automatic Jonathan C. Lansey 28 Apr 2010 10:08:16
compare Jonathan C. Lansey 28 Apr 2010 10:08:16
visualize Jonathan C. Lansey 28 Apr 2010 10:08:16
pdf Jonathan C. Lansey 28 Apr 2010 10:08:16
probability density function Jonathan C. Lansey 28 Apr 2010 10:08:16
mean Jonathan C. Lansey 28 Apr 2010 10:08:16
distribution Jonathan C. Lansey 28 Apr 2010 10:08:16
statistics Jonathan C. Lansey 28 Apr 2010 10:08:16
self_rating Matt Fig 30 Apr 2010 12:19:37

Contact us at files@mathworks.com