필터 지우기
필터 지우기

How to plot the correct axes for a heatmap

조회 수: 11 (최근 30일)
Yeping Sun
Yeping Sun 2016년 8월 14일
댓글: Image Analyst 2016년 8월 15일
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?

채택된 답변

John BG
John BG 2016년 8월 14일
Yeping
the range in imshow( , ,[range]) does not change the axis ticking.
For such purpose, to change the X axis ticks to [-2.5 2.3] and Y axis to [-2.8 2.3] you have to capture the axis handle, and access the right fields called XTickLabel and YTickLabel.
First.-
modify your code, where you imshow the heatMap adding the handle or pointer f3:
f3=figure(3);imshow(heatMap, []); axis on; colormap(hot(256)); colorbar;
now capture the handle to the axis of the figure 3:
ax3=f3.CurrentAxes
have a look to the current X ticking:
Lx=ax3.XTickLabel
Lx =
'100'
'200'
'300'
'400'
'500'
'600'
'700'
You say you want [-2.3 2.5] range, let's have the X axis ticking with same amount as it is for [100 700], with numel(Lx)
Lx2=linspace(-2.3,2.5,numel(Lx))
Because cells have variable length, use this loop to have '-' and other length variations correctly converted to cell type:
Lx_cell={};
for k=1:1:numel(Lx)
L1=num2str(Lx2(k));
Lx_cell=[Lx_cell L1];
end
Lx_cell=Lx_cell';
Now directly apply the new tick labels
f3.CurrentAxes.XTickLabel=Lx_cell
f3 =
Figure (3) with properties:
Number: 3.00
Name: ''
Color: [0.94 0.94 0.94]
Position: [501.00 161.00 872.00 795.00]
Units: 'pixels'
the command window doesn't show, but now
f3.CurrentAxes.XTickLabel
ans =
'-2.3'
'-1.5'
'-0.7'
'0.1'
'0.9'
'1.7'
'2.5'
check your figure
And for the Y axis
Ly2=linspace(-2.8,2.3,7)
Ly_cell={};
for k=1:1:7
L2=num2str(Ly2(k));
Ly_cell=[Ly_cell ;L2];
end
f3.CurrentAxes.YTickLabel=Ly_cell
Yeping
If you find my answer useful would you please be so kind to mark it as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG
  댓글 수: 1
Image Analyst
Image Analyst 2016년 8월 15일
The gray level range doesn't change the tick marks and labels, but the XData and YData properties do. So that makes it real easy - you don't need to fool with tick labels manually.
imshow(heatMap, [], 'XData', h.XBinEdges, 'YData', h.YBinEdges);

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Image Analyst
Image Analyst 2016년 8월 14일
Try adding
caxis([0, 13]);

Image Analyst
Image Analyst 2016년 8월 14일
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
Yeping Sun
Yeping Sun 2016년 8월 15일
By this code I get the attached image. Now the axes are correct. But I think your color gradient is more good-looking than mine. how do you get this color effect?
Image Analyst
Image Analyst 2016년 8월 15일
I used hot(256) to create the colormap - same as what it looks like you did. It looks like your data is different though.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by