Effacer les filtres
Effacer les filtres

How to bin 2d data?

36 vues (au cours des 30 derniers jours)
Yeping Sun
Yeping Sun le 6 Août 2016
Modifié(e) : Thorsten le 9 Août 2016
Dear all,
I have a 2-dimension coordinate set (x,y) which contains 5000 points and I want to construct a 2d map to reflect the point density distribution. firstly I want to divide both of the ranges of the two coordinate sets (-3 to2 for x; and -2.5 to 3 for y) into like 37 bins,so there will be totally 37x37 bins. Then I want to assign each point into these bins.To do that I need to construct a indicator function δkij (k=1:5000; i=1:37; j=1:37) which denotes whether point k falls in bin (i,j): if point k falls into bin (i,j), then δkij=1, otherwiseδkij=0. Then the histogram at bin (i,j) is the sum of δkij (k=1:5000). At last I wish to plot a contour map or hotmap to show the hisrogram. How does matlab realize these computation?
All the best!
Yeping Sun
  1 commentaire
Azzi Abdelmalek
Azzi Abdelmalek le 6 Août 2016
Your question is not clear, post a short example, give a brief explanation on what you want, then post the expected result

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 6 Août 2016
Modifié(e) : Image Analyst le 6 Août 2016
Try (the badly named) hist3() to construct a 2-D histogram:
% Create sample data.
numPoints = 5000;
x = -3 + 5 * rand(numPoints, 1);
y = -2.5 + 5.5 * rand(numPoints, 1);
% Now construct the histogram
xy = [x, y];
subplot(1,2,1);
hist3(xy, [37, 37]); % Plots bar chart.
subplot(1,2,2);
counts = hist3(xy, [37, 37]);
imshow(counts, []); % Show as image
colormap(hot(256));
colorbar;
axis on;
  8 commentaires
Yeping Sun
Yeping Sun le 8 Août 2016
Hi! I actually want the axes on, but I want them to be in the range of -3 to 3. Could you more specific when you said "you can send in the values to imshow() with the xaxis and y axis options"? I've tried
imshow(heatMap, [-3 3]);
it doesn't work.
By adding your code for the kernel thing, now the image becomes (the attached). It colorbar is still mad.
Image Analyst
Image Analyst le 9 Août 2016
SOrry, I meant XData and YData, like this:
imshow(heatMap, 'XData', [-3 3], 'YData', [-3,3]);
but you should really use histogram2() and get the BinEdges property and use that instead of [-3, 3] because [-3,3] may not be correct if the histogram was automatic. However if you passed in the edges, then you know what the edges are and you can use that.

Connectez-vous pour commenter.

Plus de réponses (2)

Steven Lord
Steven Lord le 6 Août 2016
Use the histogram2 function. The DisplayStyle 'tile' looks like it's exactly what you're looking for.

Thorsten
Thorsten le 9 Août 2016
Modifié(e) : Thorsten le 9 Août 2016
Have a look at this:
D = dlmread('/Users/hansen/Downloads/PC1-PC2.txt');
Nbins = 37;
H = hist3(D, {linspace(-3,3,Nbins) linspace(-3,3,Nbins)});
imshow(H, [])
colormap hot
colorbar
The values of H are in the range [0, 196], and that is the range of the colorbar.
For a contour plot, use:
contour(H)

Community Treasure Hunt

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

Start Hunting!

Translated by