How to plot the correct axes for a heatmap
Show older comments
I have a set of 2D coordinate data for 5598 points (attached) and I want to plot a heatmap to show their density distribution.First I plot the histogram by:
D=dlmread('PC1-PC2.txt');
X=D(:,1);
Y=D(:,2);
H=histogram2(X,Y,37);
and I get fig1 (attached) the h with the follow information:

Data: [5598x2 double]
Values: [37x37 double]
NumBins: [37 37]
XBinEdges: [1x38 double]
YBinEdges: [1x38 double]
BinWidth: [0.1310 0.1390]
Normalization: 'count'
FaceColor: 'auto'
EdgeColor: [0.1500 0.1500 0.1500]
The range of h.XbinEdges is -2.3 to 2.5; and the range of h.YbinEdges is -2.8 to 2.3
and a 37 x 37 matrix counts by:
counts = h.Values
After that I applied every value in the matrix "counts" to an equation, and got my target 37 by 37 matrix "G" (attached), and then I plot the heatmap
imshow(heatMap, []);
axis on;
colormap(hot(256));
colorbar;
and I got fig2 (attached)

In order to make the image larger and smoother, I used the following codes:
heatMap = imresize(G, [700, 700])
imshow(heatMap, []);
axis on;
colormap(hot(256));
colorbar;

Now the image was close to what I want, but the X and y axes ranges (0 to 7000) are meanless. I hope the axes ranges can reflect the ranges of the original coordinate data, just like the histogram (fig1): x axis range -2.3 to 2.5 and y axis range -2.8 to 2.3. So I passed these range to imshow:
imshow(heatMap, 'XData',[-2.3 2.5],'YData',[-2.8,2.3]);
axis on;
colorbar;
colormap(hot(256));
and I got fig4 (attached)

Obviously the image is wrong. Is it possible to obtain a image like fig3 but have correct axes ranges like fig4?
Accepted Answer
More Answers (2)
Image Analyst
on 14 Aug 2016
Try adding
caxis([0, 13]);
Image Analyst
on 14 Aug 2016
Yeping, this seems to work just fine.
D=dlmread('PC1-PC2.txt');
X=D(:,1);
Y=D(:,2);
h = histogram2(X,Y,37)
counts = h.Values;
G = counts/12; % Or whatever you did.
heatMap = imresize(G, [700, 700]);
imshow(heatMap, [], 'XData', h.XBinEdges, 'YData', h.YBinEdges);
axis on;
colormap(hot(256));
colorbar;
Is there anything wrong with it?

2 Comments
Yeping Sun
on 15 Aug 2016
Image Analyst
on 15 Aug 2016
I used hot(256) to create the colormap - same as what it looks like you did. It looks like your data is different though.
Categories
Find more on Color and Styling in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!