'histcount​s2(y,x,Xed​ges,Yedges​)' rather than 'histcount​s2(x,y,Xed​ges,Yedges​)' gives me the correct result, why?

I have a 2-column x-y dataset, and want to obtain a density map, thus hitscounts2 is useful to count the number of point in the bin.
When I run 'histcounts2(x,y,Xedges,Yedges)', it returns a rotated 90 degree image (left image is the scatter plot):
When I run 'histcounts2(y,x,Xedges,Yedges)', it returns the good result:
What is the reason for this?
Dataset is attached.
Code is below:
load data.txt
x = data(:,1)
y = data(:,2)
n_bins = 60;
Xedges = linspace(-100,100,n_bins + 1);
Yedges = linspace(-100,100,n_bins + 1);
[N,Xedges,Yedges] = histcounts2(x,y,Xedges,Yedges);
% [N,Xedges,Yedges] = histcounts2(y,x,Xedges,Yedges);
Xbin = linspace(min(Xedges),max(Xedges), n_bins)
Ybin = linspace(min(Yedges),max(Yedges), n_bins)
[Xv,Yv] = meshgrid(Xbin, Ybin)
figure
subplot (1,2,1)
scatter (x,y,'r.')
axis equal;
subplot (1,2,2)
surf (Xv,Yv,N);
caxis([0 40]);
view (2);
axis equal;

 Accepted Answer

Because the output of histcounts2 is an array, where rows correspond to X, and columns correspond to Y. When you use surf to display an array, rows corespond to the Y axis, and columns correspond to the X axis.
Therefore, if you use surf to view the output of histcounts2, the resulting data will be flipped along the line x=y when compared to the original data inputted to histcounts2.

More Answers (0)

Categories

Products

Release

R2018a

Community Treasure Hunt

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

Start Hunting!