Matlab-创建热图以可视化2D点数据的密度 [英] Matlab - Creating a heatmap to visualize density of 2D point data

查看:196
本文介绍了Matlab-创建热图以可视化2D点数据的密度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个N x N数组,我想生成一个热图,以这种方式可视化数据:

Given a N x N array I want to generate a heat map that visualizes data in such a way:

鉴于下面的源图像,我创建了一个人口稀少的N X N数组,其中包含下面列出的点. 1000x800阵列中的90点.

Given the source image below I created a sparsely populated N X N array that contained the points listed below. 90 points in a 1000x800 array.

在网上研究如何生成这样的热图时,我偶然发现仅使用色图来获得令人失望的结果.

When researching online how to generate such a heatmap I stumbled on using colormap only to get disappointing results.

colormap('hot');   % set colormap
imagesc(points);        % draw image and scale colormap to values range
colorbar;  

我的结果令人失望.

我还需要哪些其他选择才能使自己的形象与上一幅形象相似?

What other alternatives do I have to make my image above resemble the top one?

推荐答案

有几种不同的方法可以将分散的或稀疏的矩阵数据转换为热图,以更好地显示点密度.我将在此处显示的示例将从分散的数据开始,因此,如果您已经具有2D矩阵/直方图中的数据,则可以跳过初始步骤...

There are a few different ways you can convert scattered or sparse matrix data into a heatmap that provides some better visualization of the point density. The examples I'll show here will start with scattered data, so if you already have data in a 2D matrix/histogram you can skip the initial steps...

如果您分散的数据非常密集,则可能只需要一个简单的2D直方图.您可以创建网格覆盖分散的点(以您选择的分辨率),并使用

If your scattered data is rather dense, a simple 2D histogram may be all you need. You can create a grid covering your scattered points (at a resolution of your choosing) and bin your data in the x and y directions using histcounts2:

% Normally distributed sample points:
x = randn(1, 10000);
y = randn(1, 10000);

% Bin the data:
pts = linspace(-4, 4, 101);
N = histcounts2(y(:), x(:), pts, pts);

% Plot scattered data (for comparison):
subplot(1, 2, 1);
scatter(x, y, 'r.');
axis equal;
set(gca, 'XLim', pts([1 end]), 'YLim', pts([1 end]));

% Plot heatmap:
subplot(1, 2, 2);
imagesc(pts, pts, N);
axis equal;
set(gca, 'XLim', pts([1 end]), 'YLim', pts([1 end]), 'YDir', 'normal');

这是结果图:

如果分散的数据比较稀疏,您仍然可以如上所述创建直方图,但是随后过滤结果以使其平滑.如果您有

If your scattered data is rather sparse, you can still create a histogram as above, but then filter the result to smooth it out. You could use imdilate if you have the Image Processing Toolbox, or create a filter matrix and use conv2. Here's an example of the latter:

% Normally distributed sample points:
x = randn(1, 100);
y = randn(1, 100);

% Bin the data:
pts = linspace(-3, 3, 101);
N = histcounts2(y(:), x(:), pts, pts);

% Create Gaussian filter matrix:
[xG, yG] = meshgrid(-5:5);
sigma = 2.5;
g = exp(-xG.^2./(2.*sigma.^2)-yG.^2./(2.*sigma.^2));
g = g./sum(g(:));

% Plot scattered data (for comparison):
subplot(1, 2, 1);
scatter(x, y, 'r.');
axis equal;
set(gca, 'XLim', pts([1 end]), 'YLim', pts([1 end]));

% Plot heatmap:
subplot(1, 2, 2);
imagesc(pts, pts, conv2(N, g, 'same'));
axis equal;
set(gca, 'XLim', pts([1 end]), 'YLim', pts([1 end]), 'YDir', 'normal');

这是结果图:

从上方的稀疏直方图开始,您可以使用 bwdist 图像处理工具箱中的a>创建数据的距离变换.这将根据每个像素与最近的非零像素之间的距离为其分配一个值.

Starting with a sparse histogram from above, you could use bwdist from the Image Processing Toolbox to create a distance transform of the data. This would assign each pixel a value based on its distance from the nearest nonzero pixel.

或者,您可以通过创建网格来避免计算2D直方图>使用 统计信息工具箱中的pdist2 .这是一个示例(使用与上述相同的示例数据):

Alternatively, you could avoid computing the 2D histogram by creating a grid covering your scattered points and computing the minimum distance from each grid point to one of your scattered points using pdist2 from the Statistics Toolbox. Here's an example (using the same sample data as above):

% Generate grid and compute minimum distance:
pts = linspace(-3, 3, 101);
[X, Y] = meshgrid(pts);
D = pdist2([x(:) y(:)], [X(:) Y(:)], 'euclidean', 'Smallest', 1);

% Plot scattered data:
subplot(1, 2, 1);
scatter(x, y, 'r.');
axis equal;
set(gca, 'XLim', pts([1 end]), 'YLim', pts([1 end]));

% Plot heatmap:
subplot(1, 2, 2);
imagesc(pts, pts, reshape(D, size(X)));
axis equal;
set(gca, 'XLim', pts([1 end]), 'YLim', pts([1 end]), 'YDir', 'normal');
colormap(flip(parula(), 1));

这是结果图:

这篇关于Matlab-创建热图以可视化2D点数据的密度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆