Plot and compare histograms; pretty by default

Compares multiple sets of data elegantly. Set bins and axis bounds to be appropriate for the data.
9.1K Downloads
Updated 16 Sep 2015

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

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

Cite As

Jonathan C. Lansey (2024). Plot and compare histograms; pretty by default (https://www.mathworks.com/matlabcentral/fileexchange/27388-plot-and-compare-histograms-pretty-by-default), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Data Distribution Plots in Help Center and MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.13.0.0

Updated default bin parameter and implemented better handling of empty sets.
General update with axis tweaks etc.

1.12.0.0

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

1.11.0.0

Fixed minor colormap issue for 'colorm' option.

1.10.0.0

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.

1.9.0.0

changed the name of the function to be more clear

1.8.0.0

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

1.7.0.0

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

1.4.0.0

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

1.3.0.0

implemented ''npoints' for passing in structures.

1.2.0.0

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

1.1.0.0

I forgot to include the linspecer function, sorry!

1.0.0.0