Dear all,
I have a 2-dimension coordinate set (x,y) which contains 5000 points and I want to construct a 2d map to reflect the point density distribution. firstly I want to divide both of the ranges of the two coordinate sets (-3 to2 for x; and -2.5 to 3 for y) into like 37 bins,so there will be totally 37x37 bins. Then I want to assign each point into these bins.To do that I need to construct a indicator function δkij (k=1:5000; i=1:37; j=1:37) which denotes whether point k falls in bin (i,j): if point k falls into bin (i,j), then δkij=1, otherwiseδkij=0. Then the histogram at bin (i,j) is the sum of δkij (k=1:5000). At last I wish to plot a contour map or hotmap to show the hisrogram. How does matlab realize these computation?
All the best!
Yeping Sun

1 件のコメント

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 8 月 6 日
Your question is not clear, post a short example, give a brief explanation on what you want, then post the expected result

サインインしてコメントする。

 採用された回答

Image Analyst
Image Analyst 2016 年 8 月 6 日
編集済み: Image Analyst 2016 年 8 月 6 日

1 投票

Try (the badly named) hist3() to construct a 2-D histogram:
% Create sample data.
numPoints = 5000;
x = -3 + 5 * rand(numPoints, 1);
y = -2.5 + 5.5 * rand(numPoints, 1);
% Now construct the histogram
xy = [x, y];
subplot(1,2,1);
hist3(xy, [37, 37]); % Plots bar chart.
subplot(1,2,2);
counts = hist3(xy, [37, 37]);
imshow(counts, []); % Show as image
colormap(hot(256));
colorbar;
axis on;

8 件のコメント

Yeping Sun
Yeping Sun 2016 年 8 月 6 日
Hi, thank you for the answer. The bar chart is what I want, but I still want a hotmap like the attched one. Could matlab plot this?
Image Analyst
Image Analyst 2016 年 8 月 6 日
What attached one? If you display the histogram as an image like I did in the right plot, that is like a "heatmap" so I don't really know why you're still asking for one.
Like Steve says, there is a new function (since R2015b) with a better name, histogram2(), and it's in base MATLAB, not in a toolbox, so you can use that.
Yeping Sun
Yeping Sun 2016 年 8 月 7 日
my data PC1_PC2 is attached. They are 2D coordinates for 5598 points. What I finnally want is the image attached, which reflects the density distribution of these points. Can the image be prepared in matlab?
Image Analyst
Image Analyst 2016 年 8 月 7 日
Increase the resolution and then blur. See code:
% Create sample data.
numPoints = 500;
x = -3 + 5 * rand(numPoints, 1);
y = -2.5 + 5.5 * rand(numPoints, 1);
% Now construct the histogram
xy = [x, y];
subplot(2, 2, 1);
hist3(xy, [37, 37]); % Plots bar chart.
subplot(2, 2, 2);
counts = hist3(xy, [37, 37]);
imshow(counts, []); % Show as image
axis on;
colormap(hot(256));
colorbar;
% Now make it bigger and blurrier
% First increase the size.
heatMap = imresize(counts, [700, 700]);
subplot(2, 2, 3);
imshow(heatMap, []); % Show as image
axis on;
colormap(hot(256));
colorbar;
% Now blur.
windowSize = 51;
heatMap = imfilter(heatMap, ones(windowSize));
subplot(2, 2, 4);
imshow(heatMap, []); % Show as image
axis on;
colormap(hot(256));
colorbar;
Yeping Sun
Yeping Sun 2016 年 8 月 8 日
Thank you. Using your code my data was drawn like the figure attached. The bottom right image is close to my target figure now. But I don't understand the meaning of its axis range. In my data, the range of x and y is -2.2 to 2.5 and -2.7 to 2.3,respectively, and it is correctly reflected in the upper left figure (the histogram),but why in this bottom right image the ranges of the x and y axes are 200 to 600? And the value range of the color bar for this image is also wierd. The maximum of the counts is less than 200, why the range in the color bar is 0.5~2x10^5?
Image Analyst
Image Analyst 2016 年 8 月 8 日
編集済み: Image Analyst 2016 年 8 月 8 日
You can turn the axes labels off if you want with "axis off". Or you can send in the values to imshow() with the xaxis and y axis options.
In order to get soft, blurry shapes instead of the blocky look caused by the low 37x37 resolution, we had to increase the resolution.
To make your colorbar scale the same as the other one, you need to make the kernel sum to 1 so that imfilter() takes the average instead of the sum
kernel = ones(windowSize) / windowSize^2;
heatMap = imfilter(heatMap, kernel);
If that works, then maybe you can "Accept this answer".
Yeping Sun
Yeping Sun 2016 年 8 月 8 日
Hi! I actually want the axes on, but I want them to be in the range of -3 to 3. Could you more specific when you said "you can send in the values to imshow() with the xaxis and y axis options"? I've tried
imshow(heatMap, [-3 3]);
it doesn't work.
By adding your code for the kernel thing, now the image becomes (the attached). It colorbar is still mad.
Image Analyst
Image Analyst 2016 年 8 月 9 日
SOrry, I meant XData and YData, like this:
imshow(heatMap, 'XData', [-3 3], 'YData', [-3,3]);
but you should really use histogram2() and get the BinEdges property and use that instead of [-3, 3] because [-3,3] may not be correct if the histogram was automatic. However if you passed in the edges, then you know what the edges are and you can use that.

サインインしてコメントする。

その他の回答 (2 件)

Steven Lord
Steven Lord 2016 年 8 月 6 日

0 投票

Use the histogram2 function. The DisplayStyle 'tile' looks like it's exactly what you're looking for.
Thorsten
Thorsten 2016 年 8 月 9 日
編集済み: Thorsten 2016 年 8 月 9 日

0 投票

Have a look at this:
D = dlmread('/Users/hansen/Downloads/PC1-PC2.txt');
Nbins = 37;
H = hist3(D, {linspace(-3,3,Nbins) linspace(-3,3,Nbins)});
imshow(H, [])
colormap hot
colorbar
The values of H are in the range [0, 196], and that is the range of the colorbar.
For a contour plot, use:
contour(H)

カテゴリ

ヘルプ センター および File ExchangeData Distribution Plots についてさらに検索

質問済み:

2016 年 8 月 6 日

編集済み:

2016 年 8 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by