'histcounts2(y,x,Xedges,Yedges)' rather than 'histcounts2(x,y,Xedges,Yedges)' gives me the correct result, why?
Show older comments
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
More Answers (0)
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!