Clear Filters
Clear Filters

Why does histogram data from hist3 have to be transposed?

2 views (last 30 days)
I just learnt how to use hist3 and I don't understand a couple of lines in the example code that Matlab help provided. I've copy pasted the example for plotting a density histogram with intensity map (an intensity map is what I'm interested in plotting actually). The two lines that I don't understand are: 1. n1 = n'; --> why does the histogram have to be transposed? 2. h.ZData = ones(size(n1)) * -max(max(n)); --> what does this line do?
%Load the sample data.
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);
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);

Accepted Answer

Thorsten
Thorsten on 26 Aug 2016
Edited: Thorsten on 26 Aug 2016
1. 1. n1 = n'; --> why does the histogram have to be transposed?
Because dat is defined as
dat = [-y, x]
but the grid is defined with xb from the first column (that is -y) and yb from the second column of dat, which is x:
xb = linspace(min(dat(:,1)),max(dat(:,1)),size(n,1)+1);
yb = linspace(min(dat(:,2)),max(dat(:,2)),size(n,1)+1);
To get the right mapping for the x and y axis in the plot
h = pcolor(xb,yb,n1);
the n1 has to be transposed before.
2. h.ZData = ones(size(n1)) * -max(max(n)); --> what does this line do?
This moves the pcolor plot down by max(max(n) to appear below the hist3 plot.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!