web-dev-qa-db-ja.com

Matlab-2Dポイントデータの密度を視覚化するヒートマップの作成

N x N配列が与えられた場合、次のような方法でデータを視覚化するヒートマップを生成したいと思います。

enter image description here

以下のソースイメージを前提として、以下にリストされているポイントを含む、人口がまばらなN XN配列を作成しました。 1000x800配列で90ポイント。

enter image description here

そのようなヒートマップを生成する方法をオンラインで調べているとき、私は残念な結果を得るためだけにカラーマップを使用することに気づきました。

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

かなり残念な結果になりました。

enter image description here

上記の画像を一番上の画像に似せるには、他にどのような選択肢が必要ですか?

6

分散またはスパース行列データをヒートマップに変換して、ポイント密度をより適切に視覚化する方法はいくつかあります。ここで示す例は、分散データから開始するため、2Dマトリックス/ヒストグラムにすでにデータがある場合は、最初の手順をスキップできます...

ヒストグラム:

分散データがかなり密集している場合は、単純な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');

そして、これが結果のプロットです:

enter image description here

ヒストグラム+フィルタリング:

分散データがかなりまばらな場合でも、上記のようにヒストグラムを作成できますが、結果をフィルタリングして平滑化します。 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');

そして、これが結果のプロットです:

enter image description here

距離変換:

上からのスパースヒストグラムから始めて、 Image Processing Toolboxbwdist を使用して、の距離変換を作成できます。データ。これにより、各ピクセルに、最も近い非ゼロピクセルからの距離に基づいた値が割り当てられます。

または、 グリッドを作成 散乱点をカバーし、 を使用して各グリッド点から散乱点の1つまでの最小距離を計算することにより、2Dヒストグラムの計算を回避できます。 pdist2Statistics Toolbox から。次に例を示します(上記と同じサンプルデータを使用)。

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

そして、これが結果のプロットです:

enter image description here

5
gnovice

最初に、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;
1
beesleep