How to change heatmap xtick labels? Also, how to overlay (hold on) some dots over the heatmap?
7 次查看(过去 30 天)
显示 更早的评论
My intention is to get a plot like this:
I have a matrix similar to that for which I want to plot the correlation coefficients and pvalue<0.05 in a similar manner.
This is the matrix: (4x5)
R=[0.1 0.3 0.5 0.7 0.9;0.1 0.2 NaN 0.4 0.5;0.1 0.3 0.2 NaN 0.1;0.1 NaN NaN NaN 0.9]; %Correlation coefficients
P=[0 0.2 0.2 0.4 0.02;0.1 0.2 NaN 0.4 0.5;0.1 0.3 0.2 NaN 0.1;0.1 NaN NaN NaN 0.9]; %P values
I wanted to plot this data similar to the image above with xticklabels too. I tried this:
heatmap(R);
xticks(1:5); % note that image's ticks are aligned to the faces, pcolor's are aligned to the vertices
xticklabels({'A','B','C','D','E'});
xticks(1:4); % note that image's ticks are aligned to the faces, pcolor's are aligned to the vertices
xticklabels({'A','B','C','D'});
hold on;
mask=P<0.05;
x=1:5;
y=1:4;
[X,Y]=meshgrid(x,y);
stipple(X,Y,mask);
Is there any other easy way to do the same? I also tried pcolor function but it interpolates the values rather than giving exact values.
0 个评论
采纳的回答
Adam Danz
2021-9-27
Heatmaps are difficult to modify and customize. I recommend using imagesc() instead. See inline comments below and additional comments at the end.
R=[0.1 0.3 0.5 0.7 0.9;0.1 0.2 NaN 0.4 0.5;0.1 0.3 0.2 NaN 0.1;0.1 NaN NaN NaN 0.9]; %Correlation coefficients
P=[0 0.2 0.2 0.4 0.02;0.1 0.2 NaN 0.4 0.5;0.1 0.3 0.2 NaN 0.1;0.1 NaN NaN NaN 0.9]; %P values
fig = figure();
ax = axes(fig);
imagesc(ax, R)
Rsize = size(R);
% Tick labels assume there are 5 x-ticks and 4 y-ticks.
set(ax, 'xtick', 1:Rsize(2), 'xticklabel', ["A" "B" "C" "D" "E"], ...
'ytick', 1:Rsize(1), 'yticklabel', ["W" "X" "Y" "Z"])
% Matlab R2021a or later
xline(ax, .5:1:Rsize(2)+1, 'k-', 'Alpha', 1)
yline(ax, .5:1:Rsize(1)+1, 'k-', 'Alpha', 1)
% For Matlab R2018b to R2020b
% arrayfun(@(x)xline(ax, x, 'k-', 'Alpha', 1), .5:1:Rsize(2)+1)
% arrayfun(@(y)yline(ax, y, 'k-', 'Alpha', 1), .5:1:Rsize(1)+1)
% For Matlab prior to R2018b, use plot() or line() with xlim / ylim
% Add dots for P values less than 0.5
hold(ax,'on')
mask = P<0.5;
Psize = size(P);
[X,Y]=meshgrid(1:Psize(2), 1:Psize(1));
plot(ax, X(mask), Y(mask), 'k.', 'MarkerSize', 30)
colormap(ax, 'jet')
colorbar(ax)
caxis(ax, [0,1]) % set the colorbar & color limits
By the way, here's how to label the heatmap ticks,
heatmap(R,'XDisplayLabels', {'A','B','C','D','E'},'YDisplayLabels',{'W','X','Y','Z'});
2 个评论
Adam Danz
2021-9-28
imagesc assigns the lowest color value to NaN values. If you want the NaN boxes to be white,
colormap(ax, [ones(1,3);jet(255)])
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!