Histograms

Functions for Creating Histograms

Histograms show the distribution of data values across a data range. They do this by dividing the data range into a certain number of intervals (called "binning" the data), tabulating the number of values that fall into each interval (or "bin"), and plotting the values in the bins using bars or wedges of varying height. The functions that create histograms are hist and rose.

Function

Description

hist

Displays data in a Cartesian coordinate system.

rose

Displays data in a polar coordinate system.

You can specify the number of bins to use as a scalar second argument. If omitted, the default is 10 (hist) or 20 (rose). Data values passed to hist can be in any units and can be n-by-m, but rose expects values to be in radians in a 1-by-n or n-by-1 vector. The height (or length when using rose) of the bins represents the number of values that fall in each bin. You can also vary the size of bins by specifying a vector for apportioning bin widths as the second argument.

Histograms in Cartesian Coordinates

The hist function shows the distribution of the elements in Y as a histogram with equally spaced bins between the minimum and maximum values in Y. If Y is a vector and is the only argument, hist creates up to 10 bins. For example:

yn = randn(10000,1);
hist(yn)

generates 10,000 random numbers and creates a histogram with 10 bins distributed along the x-axis between the minimum and maximum values of yn.

Matrix Input Argument

When Y is a matrix, hist creates a set of bins for each column, displaying each set in a separate color. The statements

Y = randn(10000,3);
hist(Y)

create a histogram showing 10 bins for each column in Y.

Histograms in Polar Coordinates

A rose plot is a histogram created in a polar coordinate system. For example, consider samples of the wind direction taken over a 12-hour period:

wdir = [45 90 90 45 360 335 360 270 335 270 335 335];

To display this data using the rose function, convert the data to radians, and then use the data as an argument to the rose function. Increase the LineWidth property of the line to improve the visibility of the plot (findobj):

wdir = wdir * pi/180;
rose(wdir)
hline = findobj(gca,'Type','line');
set(hline,'LineWidth',1.5)

The plot shows that the wind direction was primarily 335° during the 12-hour period.

Specifying Number of Bins

hist and rose interpret their second argument in one of two ways—as the locations on the axis or the number of bins. When the second argument is a vector x, it specifies the locations on the axis and distributes the elements in length(x) bins. When the second argument is a scalar x, hist and rose distribute the elements in x bins.

For example, compare the distribution of data created by two MATLAB functions that generate random numbers. The randn function generates normally distributed random numbers, whereas the rand function generates uniformly distributed random numbers:

yn = randn(10000,1);
yu = rand(10000,1);

The first histogram displays the data distribution resulting from the randn function. The locations on the x-axis and number of bins depend on the vector x.

x = min(yn):.2:max(yn);
subplot(1,2,1)
hist(yn,x)
title('Normally Distributed Random Numbers','FontSize',16)

The second histogram displays the data distribution resulting from the rand function and explicitly creates 25 bins along the x-axis.

subplot(1,2,2)
hist(yu,25)
title('Uniformly Distributed Random Numbers','FontSize',16)

Using Data Cursors with Histograms

When you use the Data Cursor tool on a histogram plot, it customizes the data tips it displays in an appropriate way. Instead of providing x-, y-,z- coordinates, the datatips display the following information:

For example, The following figures show a line plot and a histogram of count.dat, a demo data set that contains three columns, giving hourly traffic counts at three different locations. The plots depict the sum the values over the locations. Each graph displays two datatips, but the datatips in the right-hand plot give information specific to histograms.

load count.dat
figure;
subplot(1,2,1); plot(count(:))
subplot(1,2,2); hist(count(:),5)
datacursormode on

Click to place a datatip or drag an existing one to a new location. You can add new datatips to a plot by right-clicking, selecting Create new datatip, and clicking the graph where you want to put it.

When you add datatips to histograms or bar graphs showing groups of data, you can move a datatip to any other bar by clicking inside that bar. If you use the cursor keys to shift a datatip back or forth across the graph, the datatip moves to the preceding or succeeding bar of the same color.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS