【问题标题】:Matlab - Creating a heatmap to visualize density of 2D point dataMatlab - 创建热图以可视化 2D 点数据的密度
【发布时间】:2017-10-29 02:02:17
【问题描述】:

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

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

在网上研究如何生成这样的热图时,我偶然发现使用颜色图只是为了得到令人失望的结果。

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

我得到了相当令人失望的结果。

我还有什么其他的方法可以让我上面的图片与上面的图片相似?

【问题讨论】:

    标签: matlab heatmap


    【解决方案1】:

    您可以通过几种不同的方式将分散或稀疏的矩阵数据转换为热图,从而提供更好的点密度可视化。我将在此处展示的示例将从分散数据开始,因此如果您已经有二维矩阵/直方图中的数据,则可以跳过初始步骤...

    直方图:

    如果您的分散数据相当密集,那么您可能只需要一个简单的 2D 直方图。您可以创建一个覆盖散点的网格(以您选择的分辨率)并使用 histcounts2 在 x 和 y 方向对数据进行分类:

    % 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');
    

    这是结果图:

    直方图+过滤:

    如果您的分散数据相当稀疏,您仍然可以像上面一样创建直方图,但然后过滤结果以使其平滑。如果你有Image Processing Toolbox,你可以使用imdilate,或者创建一个过滤矩阵并使用conv2。这是后者的一个例子:

    % 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');
    

    这是结果图:

    距离变换:

    从上面的稀疏直方图开始,您可以使用Image Processing Toolbox 中的bwdist 创建数据的距离变换。这将根据每个像素与最近的非零像素的距离为每个像素分配一个值。

    或者,您可以避免通过creating a grid 覆盖您的散点并使用Statistics Toolbox 中的pdist2 计算从每个网格点到您的散点之一的最小距离来计算二维直方图。这是一个示例(使用与上面相同的示例数据):

    % 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));
    

    这是结果图:

    【讨论】:

      【解决方案2】:

      您首先需要计算 xy 网格上点的密度。在这里,您只是在 matlab 中绘制任何散点图的“图像版本”。因此,在绘图之前,您需要处理数据并获取从您的点派生的密度图。 例如,您可以使用ksdensity 函数,该函数将估计网格基础上的点密度。如果您无法使用此功能,fileexchage 上有很多功能可以做同样的事情。

      % get the x y coordinates of your points
      [y,x] = find(points);
      P = [x,y];
      
      % estimate and plot the density
      [Est,XY] = ksdensity(P);
      imagesc(XY(:,1), XY(:,2),Est);
      colormap('hot');
      colorbar;
      

      【讨论】:

        猜你喜欢
        • 2013-07-08
        • 2018-07-06
        • 1970-01-01
        • 1970-01-01
        • 2020-02-06
        • 2014-04-05
        • 2020-06-22
        • 2012-07-22
        • 1970-01-01
        相关资源
        最近更新 更多