Skip to main content
v2026.11,858 entries · CC-BY 4.0

Histogram in MATLAB: histogram, histogram2 and imhist

Make a histogram in MATLAB with histogram, histogram2 and imhist: bin rules, normalization formulas and edge pitfalls, with code from MathWorks docs.

Written and maintained by CASRAI Editorial Board

Last updated

Short answer: in MATLAB, histogram(x) bins the vector x automatically and draws the bar chart; h = histogram(x) also returns a Histogram object whose Values and BinEdges properties hold the counts and bin boundaries. This guide covers the single-variable histogram function, normalization, bin control, 3-D histograms with histogram2 and image histograms with imhist, and is written for researchers who need to produce and read a distribution plot correctly.

What it is used for in research

A histogram shows how observations of one continuous variable are distributed across bins: the height of each bar reflects how many values (or what share of them) fall in that bin. Researchers use it to check the shape of a distribution before choosing a statistical test, to spot skew or more than one peak (see left-skewed histograms and multimodal distributions), and to compare two samples. For the underlying idea of bins and widths, see how to calculate class width.

Install or access (official source only)

histogram and histogram2 are part of core MATLAB graphics; imhist belongs to the Image Processing Toolbox. Obtain MATLAB and its documentation from MathWorks (mathworks.com). Documentation pages listed under Sources were read on 2026-11-08.

Step-by-step: a histogram in MATLAB

  1. Create or load a numeric vector. The documentation’s first example generates 10,000 normally distributed values: x = randn(10000,1);
  2. Plot it: h = histogram(x). MATLAB chooses the number of bins automatically (the default BinMethod is "auto"); the documented example gives 37 bins, readable as h.NumBins.
  3. Read the results: counts = h.Values; returns the bar heights and h.BinEdges the bin boundaries.
  4. Label the axes with xlabel and ylabel before exporting.

Histogram syntax variants

Call What it does (per MathWorks documentation)
histogram(X) Bins X automatically
histogram(X,nbins) Uses a specified number of bins
histogram(X,edges) Uses your bin edges; edges(1) is the leading edge of the first bin, edges(end) the trailing edge of the last
histogram('BinEdges',edges,'BinCounts',counts) Plots counts you already have, without binning data
histogram(C) Plots a categorical array
histogram(ax,___) Draws into a chosen axes

Control the bins

For numeric data, BinMethod accepts "auto" (default), "scott", "fd", "integers", "sturges" and "sqrt". BinWidth takes a positive scalar, with a maximum of 65,536 bins. BinLimits takes [bmin,bmax].

Two pitfalls. First, each bin includes its leading edge but not its trailing edge, except the last bin, which includes both. Second, with BinLimits only data with X >= bmin and X <= bmax is plotted, so values outside the limits silently drop out of the chart.

Normalize a histogram (probability, pdf and more)

The Normalization property defaults to "count". With ci the count in bin i, N the number of observations and wi the bin width, the documented options are:

Normalization Bar height
"count" (default) ci
"probability" ci / N
"percentage" 100 × ci / N
"countdensity" ci / wi
"pdf" ci / (N × wi)
"cumcount" cumulative count
"cdf" cumulative relative count

Use "pdf" when you want to overlay a fitted density, because the bar areas sum to 1; use "probability" or "percentage" to compare samples of different sizes. MathWorks notes that histogram ignores NaN and NaT values, but NaN, NaT, Inf and -Inf are still counted in the total used by normalizations based on the number of elements, such as "probability". For the density idea itself see histograms and the normal distribution.

Plot multiple histograms in one figure

The documentation’s example draws two histograms on the same axes with hold on, then gives both the same Normalization ('probability') and BinWidth (0.25) so they can be compared:

x = randn(2000,1);
y = 1 + randn(5000,1);
h1 = histogram(x);
hold on
h2 = histogram(y);
h1.Normalization = 'probability';
h1.BinWidth = 0.25;
h2.Normalization = 'probability';
h2.BinWidth = 0.25;

The default FaceAlpha is 0.6, so overlapping bars stay visible.

3-D histogram with histogram2

A histogram is one-dimensional by default; histogram2 draws a bivariate histogram of two variables. Syntax: histogram2(X,Y), histogram2(X,Y,nbins), histogram2(X,Y,Xedges,Yedges), or histogram2('XBinEdges',Xedges,'YBinEdges',Yedges,'BinCounts',counts). The default DisplayStyle is 'bar3' (3-D bars); 'tile' draws colored tiles, and the documentation’s example follows it with view(2) for a flat top-down view. The normalization list is the same as above, and "countdensity" divides by bin area.

x = randn(10000,1);
y = randn(10000,1);
h = histogram2(x,y);
xlabel('x'); ylabel('y')

Image histogram with imhist

For image intensities, [counts,binLocations] = imhist(I) returns the counts and bin locations; imhist(I,n) sets n bins and imhist(X,cmap) handles indexed images. Grayscale images default to 256 bins and binary images to 2. A uint8 image spans 0 to 255; double and single images are expected in the range 0 to 1.

Output and how to read it

The x-axis is the variable, the y-axis the quantity set by Normalization. Check three things: whether the shape is roughly symmetric or skewed, whether there is more than one peak, and whether the bin width is hiding or inventing structure (rerun with a smaller and larger BinWidth). Heights are only comparable between histograms when they share normalization and bin edges.

Troubleshooting

  • Data seems missing: check BinLimits and your custom edges; values outside them are not drawn.
  • Counts at a boundary look off by one bin: remember leading edges are inclusive and trailing edges are not (last bin excepted).
  • Probabilities do not sum as expected: NaN and Inf values still count toward N in "probability" normalization.
  • Two histograms are not comparable: give them the same Normalization and BinWidth, as in the example above.

Free alternatives

Environment Where to start
Python (NumPy) NumPy histogram
Python (Matplotlib) Histogram in Matplotlib
R Histograms in R
Spreadsheet Histograms in Excel

FAQ

Can you provide some examples of histograms?

Yes: the vector example in the step-by-step section, the two-sample overlay and the histogram2 example above are all taken from the MathWorks documentation.

Is a histogram 2D or 3D?

A standard histogram is two-dimensional on screen (one variable on the x-axis, counts on the y-axis). A bivariate histogram of two variables is drawn in 3-D with histogram2.

How do I create a 3-D histogram in MATLAB?

Call histogram2(x,y) with two equal-length vectors; the default 'bar3' display style draws 3-D bars.

Why is it called a histogram?

Histogram is the standard term for a chart whose bars show how many observations fall into each bin of a continuous variable, as distinct from a bar chart of separate categories.

When should you not use a histogram?

Avoid it for categorical group comparisons, for very small samples where bin choice dominates the picture, and when you need exact values rather than shape.

How do I create a matrix in MATLAB?

Use square brackets, separating rows with semicolons: A = [1 2 3; 4 5 6] creates a 2-by-3 matrix.

How do I plot an image histogram in MATLAB?

With the Image Processing Toolbox, use imhist(I) as described above.

How to create a histogram in Python or Stata?

For Python see the Matplotlib and NumPy guides. In Stata the command is histogram, followed by the variable name.

Sources (official docs)

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Histogram in MATLAB: histogram, histogram2 and imhist

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →