Creating a heatmap to visualize denisity of 2D point data

조회 수: 14 (최근 30일)
Kyle
Kyle 2015년 6월 18일
댓글: Cai Chin 2020년 11월 15일
Hello,
I am trying to create a heat map from an Mx2 matrix of point data. The point data represents spatial locations and I am attempting to create a heat map that highlights densely-clustered points from sparsely-clustered points. the data is stored in a variable called points. points(:,1) is x data and points(:,2) is y data. when I type HeatMap(points) I get useless information. Is there a way I can visualize the density of these points in a heat map? I tried hist3, but it doesn't represent the data the way I would like.
Thanks,
Kyle

채택된 답변

Walter Roberson
Walter Roberson 2015년 6월 19일
grid = 256; %refinement of map
minvals = min(points);
maxvals = max(points);
rangevals = maxvals - minvals;
xidx = 1 + round((points(:,1) - minvals(1)) ./ rangevals(1) * (grid-1));
yidx = 1 + round((points(:,2) - minvals(2)) ./ rangevals(2) * (grid-1));
density = accumarray([yidx, xidx], 1, [grid,grid]); %note y is rows, x is cols
imagesc(density, 'xdata', [minvals(1), maxvals(1)], 'ydata', [minvals(2), maxvals(2)]);
(This will make the image slightly larger than would be correct. It's probably not worth correcting for.)
  댓글 수: 3
Kyle
Kyle 2015년 6월 23일
Got it! All I needed to do was this: set(gca,'YDir','normal') after your code. Thanks!
Cai Chin
Cai Chin 2020년 11월 15일
Hi, I am trying to do a very similar thing - I used your code but it only generates a blue frame instead of a colour density map. Instead of having a matrix input, I have 2 vectors 'v' and 'w' which I tried making into a matrix to fit your code.
I was wondering if you wouldn't mind please pointing out what the issue is or suggesting an alternative?
Thanks in advance.
% Convert vectors 'v' and 'w' into a matrix
points = [v, w];
% Generate colourmap of density
grid = 256; %refinement of map
minvals = min(points);
maxvals = max(points);
rangevals = maxvals - minvals;
xidx = 1 + round((points(1) - minvals(1)) ./ rangevals(1) * (grid-1));
yidx = 1 + round((points(2) - minvals(2)) ./ rangevals(2) * (grid-1));
density = accumarray([yidx, xidx], 1, [grid,grid]); %note y is rows, x is cols
imagesc(density, 'xdata', [minvals(1), maxvals(1)], 'ydata', [minvals(2), maxvals(2)]);
set(gca,'YDir','normal');

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by