MATLAB histogram2 in MATLAB®
Learn how to make 9 histogram2 charts in MATLAB, then publish them to the Web with Plotly.
Histogram of Vectors
Generate 10,000 pairs of random numbers and create a bivariate histogram. The histogram2
function automatically chooses an appropriate number of bins to cover the range of values in x
and y
and show the shape of the underlying distribution.
x = randn(10000,1); y = randn(10000,1); h = histogram2(x,y)
h = Histogram2 with properties: Data: [10000x2 double] Values: [25x28 double] NumBins: [25 28] XBinEdges: [1x26 double] YBinEdges: [1x29 double] BinWidth: [0.3000 0.3000] Normalization: 'count' FaceColor: 'auto' EdgeColor: [0.1500 0.1500 0.1500] Show all properties
xlabel('x') ylabel('y') fig2plotly()
When you specify an output argument to the histogram2
function, it returns a histogram2 object. You can use this object to inspect the properties of the histogram, such as the number of bins or the width of the bins.
Find the number of histogram bins in each dimension.
nXnY = h.NumBins fig2plotly()
nXnY = 1×2
25 28
h = Histogram2 with properties: Data: [1000x2 double] Values: [5x5 double] NumBins: [5 5] XBinEdges: [-4 -2.4000 -0.8000 0.8000 2.4000 4] YBinEdges: [-4 -2.4000 -0.8000 0.8000 2.4000 4] BinWidth: [1.6000 1.6000] Normalization: 'count' FaceColor: 'auto' EdgeColor: [0.1500 0.1500 0.1500] Show all properties
counts = h.Values fig2plotly()
counts = 5×5
0 2 3 1 0
2 40 124 47 4
1 119 341 109 10
1 32 117 33 1
0 4 8 1 0
h = Histogram2 with properties: Data: [1000x2 double] Values: [15x15 double] NumBins: [15 15] XBinEdges: [1x16 double] YBinEdges: [1x16 double] BinWidth: [0.5000 0.5000] Normalization: 'count' FaceColor: 'auto' EdgeColor: [0.1500 0.1500 0.1500] Show all properties
nbins = morebins(h,'x'); nbins = morebins(h,'x') fig2plotly()
nbins = 1×2
19 15
nbins = fewerbins(h,'y'); nbins = fewerbins(h,'y') fig2plotly()
nbins = 1×2
19 11
h.NumBins = [20 10]; fig2plotly()
Color Histogram Bars by Height
Create a bivariate histogram using 1,000 normally distributed random numbers with 12 bins in each dimension. Specify FaceColor
as 'flat'
to color the histogram bars by height.
h = histogram2(randn(1000,1),randn(1000,1),[12 12],'FaceColor','flat'); colorbar fig2plotly()
Tiled Histogram View
Generate random data and plot a bivariate tiled histogram. Display the empty bins by specifying ShowEmptyBins
as 'on'
.
x = 2randn(1000,1)+2; y = 5randn(1000,1)+3; h = histogram2(x,y,'DisplayStyle','tile','ShowEmptyBins','on');
Specify Bin Edges of Histogram
Generate 1,000 pairs of random numbers and create a bivariate histogram. Specify the bin edges using two vectors, with infinitely wide bins on the boundary of the histogram to capture all outliers that do not satisfy |x|<2.
x = randn(1000,1); y = randn(1000,1); Xedges = [-Inf -2:0.4:2 Inf]; Yedges = [-Inf -2:0.4:2 Inf]; h = histogram2(x,y,Xedges,Yedges)
h = Histogram2 with properties: Data: [1000x2 double] Values: [12x12 double] NumBins: [12 12] XBinEdges: [1x13 double] YBinEdges: [1x13 double] BinWidth: 'nonuniform' Normalization: 'count' FaceColor: 'auto' EdgeColor: [0.1500 0.1500 0.1500] Show all properties
h.Normalization = 'countdensity';
fig2plotly()
Normalized Histogram
Generate 1,000 pairs of random numbers and create a bivariate histogram using the 'probability'
normalization.
x = randn(1000,1); y = randn(1000,1); h = histogram2(x,y,'Normalization','probability')
h = Histogram2 with properties: Data: [1000x2 double] Values: [15x15 double] NumBins: [15 15] XBinEdges: [1x16 double] YBinEdges: [1x16 double] BinWidth: [0.5000 0.5000] Normalization: 'probability' FaceColor: 'auto' EdgeColor: [0.1500 0.1500 0.1500] Show all properties
S = sum(h.Values(:)) fig2plotly()
S = 1
h = Histogram2 with properties: Data: [1000x2 double] Values: [15x15 double] NumBins: [15 15] XBinEdges: [1x16 double] YBinEdges: [1x16 double] BinWidth: [0.5000 0.5000] Normalization: 'count' FaceColor: 'auto' EdgeColor: [0.1500 0.1500 0.1500] Show all properties
h.FaceColor = 'flat';
fig2plotly()
Change the number of bins in each direction.
h.NumBins = [10 25]; fig2plotly()
Display the histogram as a tile plot.
h.DisplayStyle = 'tile';
view(2)
fig2plotly()
Saving and Loading Histogram2 Objects
Use the savefig
function to save a histogram2
figure.
histogram2(randn(100,1),randn(100,1)); savefig('histogram2.fig'); close gcf fig2plotly()
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: [100x2 double] Values: [7x6 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] Show all properties