histogram2

Histogram bar plot for bivariate data

Description

Bivariate histograms are a type of bar plot for numeric data that group the data into 2-D bins. After you create a histogram2 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 histogram2 function to create a Histogram2 object.

Properties

Histogram2 Properties Control histogram2 appearance and behavior

Object Functions

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

Examples

expand all

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

Generate 1,000 random numbers in each dimension and create a bivariate histogram.

x = randn(1000,1);
y = randn(1000,1);
h = histogram2(x,y)
h = 

  Histogram2 with properties:

             Data: [1000×2 double]
           Values: [15×15 double]
          NumBins: [15 15]
        XBinEdges: [1×16 double]
        YBinEdges: [1×16 double]
         BinWidth: [0.5000 0.5000]
    Normalization: 'count'
        FaceColor: 'auto'
        EdgeColor: [0.1500 0.1500 0.1500]

  Use GET to show all properties

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

h.Normalization = 'probability';

Change the bin limits in each dimension so that $-5 \le x \le 5$ and $-2 \le y \le 2$. When you set XBinLimits or YBinLimits, the XBinLimitsMode or YBinLimitsMode property automatically changes to 'manual'.

h.XBinLimits = [-5 5];
h.YBinLimits = [-2 2];

Show the empty bins.

h.ShowEmptyBins = 'on';

Change the display style to show tiles. Switch to a 2-D view.

h.DisplayStyle = 'tile';
view(2)

Use the savefig function to save a histogram2 figure.

y = histogram2(randn(100,1),randn(100,1));
savefig('histogram2.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('histogram2.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', 'histogram2')
y = 

  Histogram2 with properties:

             Data: [100×2 double]
           Values: [7×6 double]
          NumBins: [7 6]
        XBinEdges: [-3 -2 -1 0 1 2 3 4]
        YBinEdges: [-3 -2 -1 0 1 2 3]
         BinWidth: [1 1]
    Normalization: 'count'
        FaceColor: 'auto'
        EdgeColor: [0.1500 0.1500 0.1500]

  Use GET to show all properties

Introduced in R2015b

Was this topic helpful?