4.86957

4.9 | 26 ratings Rate this file 88 Downloads (last 30 days) File Size: 23.2 KB File ID: #27388
image thumbnail

Plot and compare histograms; pretty by default

by

 

28 Apr 2010 (Updated )

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)
'boxplot' adds boxplots to the graphs
'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 and Efficient 2 D Histogram, No Toolboxes Needed.

MATLAB release MATLAB 7.8 (R2009a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (39)
28 Jul 2014 Abdulghani  
03 Jun 2014 Jonathan C. Lansey

@Dan, the histograms are normalized by default to have an area of 1. You can change it to have the values 'sum' to 1 instead by choosing the parameter 'proportion' (though you aren't the first to ask me this so I'm considering changing how that parameter works)

26 May 2014 Dan

Hi - This is a great tool. I can't seem to be able to have it simply produce a normalized histogram. I've tried nhist(testdata,'normal'), nhist(testdata,'normalhist'), nhist(testdata,'separate'), but they all seem to produce a graph that is identical to just nhist(testdata). Please advise.

30 Apr 2014 Jonathan C. Lansey

Thanks for the nice comments!
@Kelly, I'll see about making the changes, thanks for the suggestion!

27 Mar 2014 Abbazaba

Thanks! You saved me a ton of work.

14 Mar 2014 Kelly Kearney

I want to love this function; its basic function is great, and it has helped me analyze datasets many times. Two things would make it better...

1) Return graphics handles! It drives me crazy that I always have to handle-dive via findobj to modify properties and analyze details after plotting.

2) Set default line width, axis font size, font name, etc. to match the user's defaults. You may prefer the bigger font size and thicker lines, but not everyone does, and it's a bit of a hassle to pass these properties with every call to nhist just to maintain no change.

05 Feb 2014 Sam Nazari

Extremely useful. I use nhist on a daily basis. Thanks.

09 Jan 2014 Luba Pesis

Great code! Very helpful. Thank you so much for sharing!

09 Jan 2014 Luba Pesis  
03 Dec 2013 Hamed  
29 Nov 2013 Carlos Rochinha

Already found a solution!
Again... Thanks for your built!

29 Nov 2013 Carlos Rochinha

Is it possible add a Kernel line to histogram?
I don't know how to do it.

Great script by the way!

27 Nov 2013 Hamed

Thanks for clarifying that Jonathan!

19 Nov 2013 nicolas

Thanks a lot for that function. Amazing built in customisation functionalities.

Nicolas K.

14 Nov 2013 Jonathan C. Lansey

@Hamed, Thanks for the detailed comment.

nhist default chooses 11 bins rather than 10 bins for your data. The last nhist bin goes from 0.99868 to 1.021 which includes a single data point (0.9987) which happens to be on the far left of that bin.

To show your data better you might try choosing much smaller bins.

nhist(a,'f',10);

or manually set the maximum x limit to 1
nhist(a,'maxx',1);

hope this helps!

@Sarvesh, I've been meaning to implement log10 axis scaling but I haven't yet, would be super-useful. Thanks for the suggestion.

13 Nov 2013 Hamed

I was using the function to compare the histogram of various data that I have and I noticed a bug in the function:
assume I have the following vector:
a= [NaN
0.775638811223563
0.973350006434936
0.943383964815490
0.962485362625822
0.948715322933571
0.962665074880784
0.966254225826858
0.934862260833738
0.939722782566127
0.976272235874088
0.959721480543415
0.970921202148258
0.976427283935447
0.936619607135822
0.892363443960771
0.967451691235654
0.986163657939425
0.947595390619081
0.980476539443364
0.975284102968746
0.900963987843455
0.925688573062275
0.936566882483817
0.962093098847172
0.926006354448264
0.909062533065121
0.936198208983877
0.932202530466075
0.894278361270968
0.967018528033492
0.913017842148726
0.958207171237400
0.843404538130596
0.929806874900785
0.927507509937351
0.900191997038867
0.822578263381990
0.998683319306805
0.897414661468888
0.965044081678391
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
0.951962073112260
0.933648805276507
0.806753966859453
0.883217399140238
0.933100903484828
0.922399197241776
0.914314867418531
0.963654633644814
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
];
Try using nhist and hist and you will see that they won't match. This vector contains numbers below 1 however when plotting histogram using nhist it will give a bin for values grater than 1 which is not correct.
Am I missing something?

19 Sep 2013 Sarvesh

Is there a way to change the xaxis into log10 or other scales before plotting? It seems that the bins are chosen in linear scale and I was wondering if there is a way to define the bins before hand?
thanks!

15 Jul 2013 R

very helpfull function..

09 Jul 2013 R  
01 Jul 2013 Jonathan C. Lansey

I just updated the function to have much nicer colors (thanks to colorbrewer). You can choose your own colors by changing the colormap and choosing the setting: 'color','colormap'

19 Jun 2013 Patrick

Very helpful function!

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 David  
14 May 2013 David  
20 Mar 2013 Jonathan C. Lansey

@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 Duane Hanselman

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

28 Feb 2013 Raz Shimoni

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

05 Nov 2012 Andrey Kan  
16 Jul 2012 Leo

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 Jonathan C. Lansey

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

26 Jun 2012 Shinobue111

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 Jiaqi

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

28 Nov 2011 Jonathan C. Lansey

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

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.

15 Sep 2011 Mark Brandon

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

09 Jun 2011 Laurent S

Well documented and many features, great job!

25 Oct 2010 Guido Lagos

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

17 Aug 2010 Ahmed  
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).

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.

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.

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

01 Jul 2013

Fixed the colors so that they look really great with the help of colorbrewer2.org. You also have the option to set your own colors now.

01 Jul 2013

Fixed minor colormap issue for 'colorm' option.

08 Jul 2013

Officially added boxplot (box and whiskers plot) to the graphs

27 Dec 2013

Updated default bin parameter and implemented better handling of empty sets.

Contact us