1. Matlab Advanced

Histogram in Matlab

Histogram is a representation of any statistical information showing the frequency of data items in successive intervals. MATLAB supports plotting histogram feature that enables the user to create a bar graph for any vector or matrix and grouping the data into bins using an automatic binning algorithm. For each bin, the area represents the frequency of occurrence of the data, not the height. It supports customization in histogram presentation.


In earlier versions, hist() and histc() were used to generate histogram plots. In later versions those functions are replaced with new functions with advanced capabilities i.e. histogram(), histcounts() and discretize().

The syntax for the above-recommended functions are as follows:

  • histogram(X, a1,a2,…..,an)
  • histcounts(X, a1,a2,…..,an)
  • discretize(X, a1,a2,…..,an)

Where X: Data input in the form of vector or matrix.

a1,a2,…..,an: Attribute values (optional)

Creation of Histogram in Matlab: MATLAB makes it a single click action to create a histogram for any data. A histogram can be created by using the inbuilt function histogram().


The below code is written to generate 100 random numbers and histogram() is used to plot a histogram for the generated data.


data = randn(100,1);
h = histogram(data)


Histogram in Matlab Example 1

Once any histogram object is created, it can be altered by altering its property values, that makes changes in the properties of bins and thus in the display.

Properties of Histogram in Matlab

Various properties that are featured for the histogram in MATLAB, are as follows:

1. Bins

NumBinsDecides the number of bins to be generated.
BinWidthDecides the width of each bin.
BinEdgesA vector of which the first element of the vector determines the edge of the first bin and the last element decides the edges of the last bin of the histogram.
BinlimitsSets the limits for the input vector/matrix values.
BinLimitModesDecides mode of setting the limits.
BinMethodChoose the algorithm to configure bin width.

2. Categories

This property allows to plot histogram for each category defined in the input categorical array. If bin count is specified, categories sets the associated category descriptions in the plot.

This property contains parameters such as mentioned below:

DisplayOrderDecides the order of bars based on height.
NumDisplayBinsDecides the number of categories to be displayed.
ShowOthersDecides about the visibility of the additional bar which contains excluded elements of selected categories.

3. Data

This value gets distributed over a histogram plot among the bins. This property consists of different parameters such as:

ValuesDecides the number of data elements to be added to a specific bin.
NormalizationApplies a specific type of normalization on the data such as count, probability, countdensity, pdf, cumcount, etc.
BinCountsAccepts the bin count as input from an external bin calculation method instead of histogram data binning.
BinCountsModeRepresents the mode of deciding bin counts.

4. Color and Styling

DisplayStyleDecides the style to impose on the histogram display.
OrientationDecides upon the orientation of the bars on the histogram plot- vertical or horizontal.
BarWidthRegulates the separation of categorical bars.
FaceColorSets the color of the bars.
EdgeColorSets the color of the edges.
FaceAlphaDecides on the transparency of the bars.
EdgeAlphaDecides on the transparency of the edges.
LineStyleSets style of the bar outlines.
LineWidthSets the width of the bar outlines.

5. Legend

This property in the MATLAB adds descriptive labels to the plots. It comprises of:

DisplayNameSets the text to be added to the description for the axes.
AnnotationControls the inclusion of the objects in a legend and sets excluded objects as an annotation object.

6. Interactivity

VisibleSets the visibility of an object.
DataTipTemplateDecides on the content that appears on a data tip.
UIContextMenuSets the context menu for an object, displayed on right-click over the object.
SelectedManages the selection mode of the object.
SelectionHighlightDecides the visibility of selection handlers around an object.

7. Callbacks

ButtonDownFcnAccepts a function as a value which is to be executed when an object is clicked.
CreateFcnAccepts a function as a value which is to be executed when an object is created.
DeleteFcnAccepts a function as a value which is to be executed when an object is deleted.

8. Callback Execution Control

InterruptibleDetermines whether the callback function can be interrupted or not.
BusyActionDetermines how the interruption in callback function will be handled.
PickablePartsSets the context menu for an object, displayed on right-click over the object.
SelectedUsed to enable/disable capturing mouse clicks.
HitTestDecides response on the captured mouse clicks on the Histogram plot.
BeingDeletedUsed to store the status of the execution of DeleteFcn callback.

9. Parent/Child

ParentAxes, Polar axes, Transform objects or Group objects are specified as a parent.
ChildThis property is a read-only element which is used to view a list of data tips that are plotted in the histogram.
HandleVisibilityAccepts a function as a value which is to be executed when an object is deleted.

10. Identifiers

TypeRepresents the type of graphic object.
TagServes as an object identifier.
UserDataStores arbitrary data on an subject

Leave a Reply

Your email address will not be published. Required fields are marked *