histogram

Histogram bar plot for numeric data

Description

Histograms are a type of bar plot for numeric data that group the data into bins. After you create a histogram object, you can modify aspects of the histogram by changing its property values. This is particularly useful for quickly modifying the properties of the bins or changing the display.

Create Object

Specify an output argument with the histogram function to create a histogram object.

Properties

Histogram Properties Control histogram appearance and behavior

Object Functions

morebins Increase number of histogram bins
fewerbins Decrease number of histogram bins

Examples

expand all

This example creates a histogram object, and then shows how to adjust the properties of the histogram to affect the output display.

Create a histogram for all of the prime numbers less than 10,000,000. Specify an output argument to return the histogram object, h.

x = primes(1e7);
h = histogram(x)
h = 

  Histogram with properties:

             Data: [1×664579 double]
           Values: [1×100 double]
          NumBins: 100
         BinEdges: [1×101 double]
         BinWidth: 100000
        BinLimits: [0 10000000]
    Normalization: 'count'
        FaceColor: 'auto'
        EdgeColor: [0 0 0]

  Use GET to show all properties

Change the normalization of the histogram by setting the Normalization property to 'probability'.

h.Normalization = 'probability';

Use horizontal bars for the histogram.

h.Orientation = 'horizontal';

Zoom in to a specific range of bins. When you set BinLimits, the BinLimitsMode automatically changes to 'manual'.

h.BinLimits = [1e6,2e6];

Zoom back out to the original bin scaling by switching BinLimitsMode back to 'auto'.

h.BinLimitsMode = 'auto';

Change the number of bins in the histogram.

h.NumBins = 200;

Create a vertical stair histogram.

h.Orientation = 'vertical';
h.DisplayStyle = 'stairs';

Use the savefig function to save a histogram figure.

y = histogram(randn(10));
savefig('histogram.fig');
clear all
close all

Use openfig to load the histogram figure back into MATLAB. openfig also returns a handle to the figure, h.

h = openfig('histogram.fig');

Use the findobj function to locate the correct object handle from the figure handle. This allows you to continue manipulating the original histogram object used to generate the figure.

y = findobj(h, 'type', 'histogram')
y = 

  Histogram with properties:

             Data: [10×10 double]
           Values: [2 17 28 32 16 3 2]
          NumBins: 7
         BinEdges: [-3 -2 -1 0 1 2 3 4]
         BinWidth: 1
        BinLimits: [-3 4]
    Normalization: 'count'
        FaceColor: 'auto'
        EdgeColor: [0 0 0]

  Use GET to show all properties

Introduced in R2014b

Was this topic helpful?