Heat map of 2-D scatter plot data

조회 수: 8 (최근 30일)
Kenny Kim
Kenny Kim 2015년 5월 15일
댓글: Walter Roberson 2015년 5월 15일
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?

채택된 답변

Walter Roberson
Walter Roberson 2015년 5월 15일
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
Kenny Kim
Kenny Kim 2015년 5월 15일
편집: Kenny Kim 2015년 5월 15일
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 2015년 5월 15일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2015년 5월 15일
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, '*')

태그

Community Treasure Hunt

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

Start Hunting!

Translated by