1

I am trying to plot the point density for a large set of points (see image). How would you do it? I tried using the code below, but I got the error Error using griddedInterpolant The grid vectors are not strictly monotonic increasing (I guess because the points are not ordered).

enter image description here

%# bin centers (integers)
xbins = floor(min(X)):step_for_plot:ceil(max(X));
ybins = floor(min(Y)):step_for_plot:ceil(max(Y));
xNumBins = numel(xbins); 
yNumBins = numel(ybins);

%# map X/Y values to bin indices
Xi = round( interp1(xbins, 1:xNumBins, X, 'linear', 'extrap') );
Yi = round( interp1(ybins, 1:yNumBins, Y, 'linear', 'extrap') );

%# limit indices to the range [1,numBins]
Xi = max( min(Xi,xNumBins), 1);
Yi = max( min(Yi,yNumBins), 1);

%# plot 2D histogram
imagesc(xbins, ybins, Data), axis on %# axis image
colormap hot; colorbar
hold on, plot(X, Y, 'b.', 'MarkerSize',1), hold off
albus_c
  • 6,292
  • 14
  • 36
  • 77
  • I dont know what you are triying to do exactly, but in your code there is written "plot 2D histogram". What about hist() and histogram() functions? – Ander Biguri Nov 04 '14 at 10:19
  • What I'm trying to do is show (for example on a colormap) the density of points per pixel. – albus_c Nov 04 '14 at 10:24
  • Do you just want to count the number of points per bin (i.e. per pixel) or are you interested in the density of the neighboring pixels as well? – Dan Nov 04 '14 at 10:26
  • 1
    Also can you provide some simple sample input data? – Dan Nov 04 '14 at 10:27
  • @Dan: I just want to plot the number of points per bin. I uploaded a dataset here: http://cl.ly/text/3e0y2m2P3q0o – albus_c Nov 04 '14 at 10:59

2 Answers2

1

I solved the problem using hist3. Here is the code I used:

n = hist3(Data,[100,100]); 
n1 = n';
n1(size(n,1) + 1, size(n,2) + 1) = 0;

xb = linspace(min(Data(:,1)),max(Data(:,1)),size(n,1)+1);
yb = linspace(min(Data(:,2)),max(Data(:,2)),size(n,1)+1);

g = figure();
h = pcolor(xb,yb,n1);
albus_c
  • 6,292
  • 14
  • 36
  • 77
0

If you just want the points per bin then why are you interpolating?

hgram = accumarray([Y(:), X(:)], 1, [yNumBins xNumBins])

otherwise perhaps an alternative for that could be to think of it as an image and blur the image. So taking hgram from above:

heatmap = conv2(hgram, kernel, 'same');
imshow(heatmap/max(heatmap(:)))

where kernel could be very simple like:

ones(n)

or more sophisticated like a gaussian filter

Community
  • 1
  • 1
Dan
  • 45,079
  • 17
  • 88
  • 157
  • `hgram` was from a previous test, and that's why it's there. I just need to plot the density of points from the `Data` matrix – albus_c Nov 04 '14 at 11:52
  • @albus_c 2 points, (1) `hgram` wasn't in your code...? and (2) you need to define what you mean by point density. This code adjusts each pixel according to the number of points in it's neighbours, that sounds like a measure of point density to me. Have you tried it on your data? – Dan Nov 04 '14 at 12:08
  • 1
    OK things were easier than expected. I solved the problem using hist3 (http://www.mathworks.se/help/stats/hist3.html). – albus_c Nov 04 '14 at 12:27
  • 1
    @albus_c then you should add the code as an answer to your own question – Dan Nov 04 '14 at 12:33