MATLAB Answers


How can I make my histogram bin edges the bin centers instead?

How can a histogram plot be modified, independent of the data, so as to make its bin centers its old bin edges?




1 Answer

Answer by MathWorks Support Team on 22 Mar 2016
 Accepted Answer

The below steps apply from MATLAB R2014b or newer. For older versions, use the appropriate "set" and "get" functions for graphics.
1) Obtain a handle to the histogram when first plotting it:
histHandle = histogram(data);
If the histogram has already been plotted in 'Figure n', obtain it in the following manner:
figHandle = figure(n);
axisHandle = figHandle.Children;
histHandle = axisHandle.Children;
2) Offset the bin edges by half the bin width:
histHandle.BinEdges = histHandle.BinEdges + histHandle.BinWidth/2;
Be careful not to eliminate a half-bin with data when doing this. If required, add an extra bin edge to the beginning of the "histHandle.BinEdges" vector.


Sign in to comment.