Heat map of 2-D scatter plot data

17 views (last 30 days)
I have a n by 2 matrix called new_data, and I want to make a heat map plotting density of scatter distribution.
I want to add values varying from 0 to 1 to another array called z that has size of (xmax-xmin,ymax-ymin) in which value of 1 would be added to the z component that matches x and y coordinate and add values less than 1 as the distance from the coordinates increase (kind of like dropping ink on paper, and less ink with increasing distance from epicenter).
Here's my code til now:
x = new_data(:,1);
y = new_data(:,2);
z = zeros(round(max(x)) - round(min(x)), round(max(y)) - round(min(y)));
for i = 1:size(new_data,1);
x(i) = x(i) - min(x);
y(i) = y(i) - min(y);
end
for i = 1:size(new_data,1);
z(round(x(i)),round(y(i))) = z(round(x(i)),round(y(i))) + 1;
end
Could anyone help me with this Gaussian value addition or provide any other ideas for plotting scatter density?

Accepted Answer

Walter Roberson
Walter Roberson on 15 May 2015
for i = 1:size(new_data,1);
d = sqrt((x(i) - round(x(i))).^2 + (y(i) - round(y(i)).^2); %distance
deltaz = exp(-d); %gets exponentially less with increasing distance
z(round(x(i)),round(y(i))) = z(round(x(i)),round(y(i))) + deltaz;
end
  2 Comments
Kenny Kim
Kenny Kim on 15 May 2015
Edited: Kenny Kim on 15 May 2015
I tried a modification of your code and it seems to work, except that my new_data has 27000 rows and thus would require 13 hours or more to process it. Changing x to x1 has nothing to do with this.
for i = 1:size(new_data,1);
for j = 1:size(z,1);
for k = 1:size(z,2);
d = sqrt((j - x1(i)).^2 + (k - y1(i)).^2); %distance
if d <= 20
deltaz = exp(-d); %gets exponentially less with increasing distance
z(j,k) = z(j,k) + deltaz;
end
end
end
end
Walter Roberson
Walter Roberson on 15 May 2015
Vectorize, Vectorize, Vectorize!
ndrow = size(new_data,1);
zrow = size(z,1);
zcol = size(z,2);
zcolvec = 1 : zcol;
for i = 1 : ndrow;
x1i = x1(i);
y1i = y1(i);
for j = 1 : zrow;
jdist2 = (j-x1i).^2;
d = sqrt(jdist2 + (zcolvec - y1i).^2);
deltaz = exp(-d);
dinrange = d <= 20;
z(j, dinrange) = z(j,dinrange) + deltaz(dinrange);
end
end

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 15 May 2015
If you have the Image Processing Toolbox, perhaps one way is to use the distance transform:
numPoints = 30;
x = rand(1,numPoints);
y = rand(1, numPoints);
rows = 240;
columns = 320;
binaryImage = false(rows, columns);
for k = 1 : length(x);
r = ceil(rows * y(k));
c = ceil(columns * x(k));
binaryImage(r, c) = true;
end
imshow(binaryImage);
edm = bwdist(binaryImage);
edmMax = max(edm(:));
edm = edmMax - edm;
imshow(edm, []);
colormap(hot(256));
colorbar;
hold on;
scatter(columns * x, rows * y, '*')

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!