How to change heatmap xtick labels? Also, how to overlay (hold on) some dots over the heatmap?

7 vues (au cours des 30 derniers jours)
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.

Réponse acceptée

Adam Danz
Adam Danz le 27 Sep 2021
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 commentaires
Abhishek Chakraborty
Abhishek Chakraborty le 28 Sep 2021
Hi. Thanks a lot for it. I was just wondering why the NaN values are shown in dark blue in the plot and not white as is the case with pcolor plots?
Adam Danz
Adam Danz le 28 Sep 2021
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)])

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Distribution Plots dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by