绘图点密度

时间:2014-11-04 10:15:59

标签: matlab plot

我试图绘制一组大点的点密度(见图)。你会怎么做?我尝试使用下面的代码,但是我收到了错误Error using griddedInterpolant The grid vectors are not strictly monotonic increasing(我猜是因为这些点没有订购)。

enter image description here

%# bin centers (integers)
xbins = floor(min(X)):step_for_plot:ceil(max(X));
ybins = floor(min(Y)):step_for_plot:ceil(max(Y));
xNumBins = numel(xbins); 
yNumBins = numel(ybins);

%# map X/Y values to bin indices
Xi = round( interp1(xbins, 1:xNumBins, X, 'linear', 'extrap') );
Yi = round( interp1(ybins, 1:yNumBins, Y, 'linear', 'extrap') );

%# limit indices to the range [1,numBins]
Xi = max( min(Xi,xNumBins), 1);
Yi = max( min(Yi,yNumBins), 1);

%# plot 2D histogram
imagesc(xbins, ybins, Data), axis on %# axis image
colormap hot; colorbar
hold on, plot(X, Y, 'b.', 'MarkerSize',1), hold off

2 个答案:

答案 0 :(得分:1)

我使用hist3解决了这个问题。这是我使用的代码:

n = hist3(Data,[100,100]); 
n1 = n';
n1(size(n,1) + 1, size(n,2) + 1) = 0;

xb = linspace(min(Data(:,1)),max(Data(:,1)),size(n,1)+1);
yb = linspace(min(Data(:,2)),max(Data(:,2)),size(n,1)+1);

g = figure();
h = pcolor(xb,yb,n1);

答案 1 :(得分:0)

如果您只想要每箱的点数,那么为什么要进行插值?

hgram = accumarray([Y(:), X(:)], 1, [yNumBins xNumBins])

否则也许替代方案可能是将其视为图像并模糊图像。所以从上面拿hgram

heatmap = conv2(hgram, kernel, 'same');
imshow(heatmap/max(heatmap(:)))

其中kernel非常简单,如:

ones(n)

或像gaussian filter

那样复杂
相关问题