Accelerating the pace of engineering and science

hist

Histogram plot (not recommended; use histogram)

hist is not recommended. Use histogram instead.

Syntax

• counts = hist(___)
• [counts,centers] = hist(___) example

Description

example

hist(x) creates a histogram bar chart of the elements in vector x. The elements in x are sorted into 10 equally spaced bins along the x-axis between the minimum and maximum values of x. hist displays bins as rectangles, such that the height of each rectangle indicates the number of elements in the bin.

If the input is a multi-column array, hist creates histograms for each column of x and overlays them onto a single plot.

If the input is of data type categorical, each bin is a category of x.

example

hist(x,nbins) sorts x into the number of bins specified by the scalar nbins.

example

hist(x,xbins) sorts x into bins with intervals or categories determined by the vector xbins.

• If xbins is a vector of evenly spaced values, then hist uses the values as the bin centers.

• If xbins is a vector of unevenly spaced values, then hist uses the midpoints between consecutive values as the bin edges.

• If x is of data type categorical, then xbins must be a categorical vector or cell array of strings that specifies categories. hist plots bars only for those categories.

The length of the vector xbins is equal to the number of bins.

example

hist(ax,___) plots into the axes specified by ax instead of into the current axes (gca). The option ax can precede any of the input argument combinations in the previous syntaxes.

counts = hist(___) returns a row vector, counts, containing the number of elements in each bin.

example

[counts,centers] = hist(___) returns an additional row vector, centers, indicating the location of each bin center on the x-axis.

Examples

expand all

Histogram of Vector

```x = [0 2 9 2 5 8 7 3 1 9 4 3 5 8 10 0 1 2 9 5 10];
hist(x)
```

hist sorts the values in x among 10 equally spaced bins between the minimum and maximum values in the vector, which are 0 and 10 in this example.

Histogram of Multiple Columns

Generate three columns of 1,000 random numbers and plot the three column overlaid histogram.

```x = randn(1000,3);
hist(x)
```

The values in x are sorted among 10 equally spaced bins between the minimum and maximum values. hist sorts and bins the columns of x separately and plots each column with a different color.

Specify Number of Histogram Bins

Plot a histogram of 1,000 random numbers sorted into 50 equally spaced bins.

```x = randn(1000,1);
nbins = 50;
hist(x,nbins)
```

Specify Histogram Bin Intervals

Plot three histograms of the same data using different bin intervals:

• In the upper subplot, specify the bin centers using a vector of evenly spaced values that span the values in x.

• In the middle subplot, specify the bin centers using a vector of evenly spaced values that do not span the values in x. The first and last bins extend to cover the minimum and maximum values in x.

• In the lower subplot, specify the bin intervals using a vector of unevenly spaced values. The hist function uses the midpoints between consecutive values as the bin edges and indicates the specified values by markers along the x-axis.

```x = randn(1000,1);
subplot(3,1,1)
xbins1 = -4:4;
hist(x,xbins1)

subplot(3,1,2)
xbins2 = -2:2;
hist(x,xbins2)

subplot(3,1,3)
xbins3 = [-4 -2.5 0 0.5 1 3];
hist(x,xbins3)
```

Specify Histogram Axes

Create a figure with two subplots. In the upper subplot, plot a histogram of 1,000 random numbers sorted into 50 equally spaced bins. In the lower subplot, plot a histogram of the same data and use bins with centers at -3, 0, and 3.

```x = randn(1000,1);
ax1 = subplot(2,1,1);
hist(ax1,x,50)

ax2 = subplot(2,1,2);
xbins = [-3 0 3];
hist(ax2,x,xbins)
```

Use hist to Calculate Only

Generate 1,000 random numbers. Count how many numbers are in each of 10 equally spaced bins. Return the bin counts and bin centers.

```x = randn(1000,1);
[counts,centers] = hist(x)
```
```counts =

4    27    88   190   270   243   123    38    13     4

centers =

Columns 1 through 7

-2.8915   -2.2105   -1.5294   -0.8484   -0.1673    0.5137    1.1947

Columns 8 through 10

1.8758    2.5568    3.2379

```

Use bar to plot the histogram.

```bar(centers,counts)
```

Specify Histogram Colors

Generate 1,000 random numbers and create a histogram.

```data = randn(1000,1);
hist(data)
```

Get the handle to the patch object that creates the histogram plot.

```h = findobj(gca,'Type','patch');
```

Set the face color of the bars plotted to an RGB triplet value of [0 0.5 0.5]. Set the edge color to white.

```h.FaceColor = [0 0.5 0.5];
h.EdgeColor = 'w';
```

Input Arguments

expand all

x — Input arrayvector or matrix

Input vector or matrix.

• If x is a vector, then hist creates one histogram.

• If x is a matrix, then hist creates a separate histogram for each column and plots the histograms using different colors.

If the input array contains NaNs or undefined categorical values, hist does not include these values in the bin counts.

If the input array contains the infinite values -Inf or Inf, then hist sorts -Inf into the first bin and Inf into the last bin. If you do not specify the bin intervals, then hist calculates the bin intervals using only the finite values in the input array.

Data Types: single|double|logical|categorical

nbins — Number of bins10 (default) | scalar

Number of bins. Input x must be numeric, not categorical.

Data Types: single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64

xbins — Bin locations or categoriesvector

Bin locations or categories, specified as a vector.

If x is numeric or logical, then xbins must be of type single or double.

• If the elements in xbins are equally spaced, then these elements are the bin centers.

• If the elements in xbins are not equally spaced, then these elements are indicated by markers along the x-axis, but are not the actual bin centers. Instead, hist calculates the bin edges as the midpoints between consecutive elements in vector xbins. To specify the bin edges directly, use histc.

• xbins must contain only finite values. The first and last bins extend to cover the minimum and maximum values in x.

If x is categorical, then xbins must be a categorical vector or cell array of strings that specifies categories. hist plots bars only for those categories specified by xbins.

ax — Axes objectaxes object

Axes object. Use ax to plot the histogram in a specific axes instead of the current axes (gca).

Output Arguments

expand all

counts — Counts of the number of elements in each binrow vector

Counts of the number of elements in each bin, returned as a row vector.

centers — Bin centers or categoriesvector

Bin centers or categories, returned as a vector. If used with the syntax [counts,centers] = hist(x,xbins), then the centers output has the same elements as the xbins input.

• If x is numeric or logical, then centers is a numeric row vector.

• If x is categorical, then centers is a cell array of strings.