File Exchange

image thumbnail

histf

version 1.0.0.0 (62.4 KB) by Chad Greene
Simply produce histograms formatted the way you like them.

28 Downloads

Updated 15 Aug 2014

View License

This function plots formatted histograms. It's similar to hist, but with more options for FaceColor, BarWidth, LineStyle, FaceAlpha, etc.

Syntax
histf(Y)
histf(Y,x)
histf(Y,nbins)
histf(...,'BarWidth',BarWidth)
histf(...,'FaceColor',FaceColor)
histf(...,'EdgeColor',EdgeColor)
histf(...,'LineStyle',LineStyle)
histf(...,'LineWidth',LineWidth)
histf(...,'FaceAlpha',FaceAlpha)
histf(...,'EdgeAlpha',EdgeAlpha)
histf(...,'Alpha',Alpha)
[n,xout,h] = histf(...)

Description
histf(Y) bins the elements in vector Y into 10 equally spaced containers and returns the number of elements in each container as a row vector. If Y is an m-by-p matrix, histf treats the columns of Y as vectors. No elements of Y can be complex or of type integer.

histf(Y,x) where x is a vector, returns the distribution of Y among length(x) bins with centers specified by x. For example, if x is a 5-element vector, histf distributes the elements of Y into five bins centered on the x-axis at the elements in x, none of which can be complex.

histf(Y,nbins) where nbins is a scalar, uses nbins number of bins.

histf(...,'BarWidth',BarWidth) specifies the width of bars as a fraction of total space available for each bar. Default BarWidth is 1.

histf(...,'FaceColor',FaceColor) specifies face color as a short name, long name, or RGB triple.

histf(...,'EdgeColor',EdgeColor) specifies edge coloras a short name, long name, or RGB triple.

histf(...,'LineStyle',LineStyle) specifies line style of bar edges. Can be '-' (default), ':', '--', '-.', or 'none'.

histf(...,'LineWidth',LineWidth) specifies line width of bar edge in points. Default LineWidth is 0.5 points.

histf(...,'FaceAlpha',FaceAlpha) specifies transparency value of bar faces. FaceAlpha must be a scalar between 0 and 1. Default value is 1.

histf(...,'EdgeAlpha',EdgeAlpha) specifies transparency value of bar edges. EdgeAlpha must be a scalar between 0 and 1. Default value is 1.

histf(...,'Alpha',Alpha) sets FaceAlpha and EdgeAlpha to Alpha.

[n,xout,h] = histf(...) returns vectors n and xout containing the frequency counts and the bin locations. h is the handle of the newly plotted histogram.

Cite As

Chad Greene (2020). histf (https://www.mathworks.com/matlabcentral/fileexchange/47552-histf), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (6)

Abhinav

Very nice function! I've made a small addition that may be interesting to some, that is another optional parameter that allows the user to normalize the bin counts. Two normalizations may be by total histogram area or by maximum bin count.

I modified the original code by parsing an additional input argument:
-------------------------
Normalize = 'none';
tmp = strcmpi(varargin,'Normalize');
if any(tmp)
Normalize = varargin{find(tmp)+1};
assert(ismember(Normalize,{'none','max','area'})==1,'Normalize must be one of: none, max or area')
tmp(find(tmp)+1)=1;
varargin = varargin(~tmp);
end
----------------------------------------

Subsequently, after the bin counts, n, and xAxis labels, xout, are determined scale the data as follows:
----------------------------------
if ~strcmp(Normalize,'none')
if strcmp(Normalize,'max')
n = n / max(n);
elseif strcmp(Normalize,'area')
n = n / sum(n);
end
end
----------------------------------

salma

Char-Thanks!

Chad Greene

Salma--That's right, this function only works for pre-2014b. For newer versions of Matlab use the histogram function.

salma

facealpha does not work, the histogram are not transparent. Is there an update for 2015 version.

tastygrain

Just what I was looking for for overlaying transparent histograms. Works perfectly.

MATLAB Release Compatibility
Created with R2012b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired by: legalpha

histf_documentation/html/