Accelerating the pace of engineering and science

hist3

Bivariate histogram

Syntax

hist3(X)
hist3(X,nbins)
hist3(X,ctrs)
hist3(X,'Edges',edges)
N = hist3(X,...)
[N,C] = hist3(X,...)
hist3(...,param1,val1,param2,val2,...)

Description

hist3(X) bins the elements of the m-by-2 matrix X into a 10-by-10 grid of equally spaced containers, and plots a histogram. Each column of X corresponds to one dimension in the bin grid.

hist3(X,nbins) plots a histogram using an nbins(1)-by-nbins(2) grid of bins. hist3(X,'Nbins',nbins) is equivalent to hist3(X,nbins).

hist3(X,ctrs), where ctrs is a two-element cell array of numeric vectors with monotonically non-decreasing values, uses a 2-D grid of bins centered on ctrs{1} in the first dimension and on ctrs{2} in the second. hist3 assigns rows of X falling outside the range of that grid to the bins along the outer edges of the grid, and ignores rows of X containing NaNs. hist3(X,'Ctrs',ctrs) is equivalent to hist3(X,ctrs).

hist3(X,'Edges',edges), where edges is a two-element cell array of numeric vectors with monotonically non-decreasing values, uses a 2-D grid of bins with edges at edges{1} in the first dimension and at edges{2} in the second. The (i, j)th bin includes the value X(k,:) if

```edges{1}(i) <= X(k,1) < edges{1}(i+1)
edges{2}(j) <= X(k,2) < edges{2}(j+1)
```

Rows of X that fall on the upper edges of the grid, edges{1}(end) or edges{2}(end), are counted in the (I,j)th or (i,J)th bins, where I and J are the lengths of edges{1} and edges{2}. hist3 does not count rows of X falling outside the range of the grid. Use -Inf and Inf in edges to include all non-NaN values.

N = hist3(X,...) returns a matrix containing the number of elements of X that fall in each bin of the grid, and does not plot the histogram.

[N,C] = hist3(X,...) returns the positions of the bin centers in a 1-by-2 cell array of numeric vectors, and does not plot the histogram. hist3(ax,X,...) plots onto an axes with handle ax instead of the current axes. See the axes reference page for more information about handles to plots.

hist3(...,param1,val1,param2,val2,...) allows you to specify graphics parameter name/value pairs to fine-tune the plot.

Examples

expand all

Plot Density Histogram with Intensity Map

```load seamount
```

Correct grid for negative y-values and draw histogram in 2D.

```hold on
dat = [-y,x];
hist3(dat)
```

Extract histogram data.

```n = hist3(dat); % default is to 10x10 bins
n1 = n';
n1(size(n,1) + 1, size(n,2) + 1) = 0;
```

Generate grid for 2-D projected view of intensities.

```xb = linspace(min(dat(:,1)),max(dat(:,1)),size(n,1)+1);
yb = linspace(min(dat(:,2)),max(dat(:,2)),size(n,1)+1);
```

Make a pseudocolor plot.

```h = pcolor(xb,yb,n1);
```

Set the z-level and colormap of the displayed grid, and display the default 3-D perspective view.

```h.ZData = ones(size(n1)) * -max(max(n));
colormap(hot) % heat map
title('Seamount:Data Point Density Histogram and Intensity Map');
grid on
view(3);
```

Histogram with Semi-Transparent Bars

```load carbig
```

Use the data to make histogram on a 7-by-7 grid of bins.

```X = [MPG,Weight];
hist3(X,[7 7]);
xlabel('MPG'); ylabel('Weight');
```

Make a histogram with semi-transparent bars.

```hist3(X,[7 7],'FaceAlpha',.65);
xlabel('MPG'); ylabel('Weight');
set(gcf,'renderer','opengl');
```

Specify bin centers, different in each direction; get back counts, but don't make the plot.

```cnt = hist3(X, {0:10:50 2000:500:5000});
```

Histogram Bars Colored According to Height

```load carbig
```

Make a histogram on a 7-by-7 grid of bins.

```X = [MPG,Weight];
hist3(X,[7 7]);
xlabel('MPG'); ylabel('Weight');
set(gcf,'renderer','opengl');
```

Color the bars based on the frequency of the observations, i.e. according to the height of the bars.

```set(get(gca,'child'),'FaceColor','interp','CDataMode','auto');
```