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

35 Ansichten (letzte 30 Tage)
Abhishek Chakraborty am 27 Sep. 2021
Kommentiert: Adam Danz am 28 Sep. 2021
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;
x=1:5;
y=1:4;
[X,Y]=meshgrid(x,y);
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 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Adam Danz am 27 Sep. 2021
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')
Psize = size(P);
[X,Y]=meshgrid(1:Psize(2), 1:Psize(1));
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 Kommentare1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
Adam Danz am 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)])

Melden Sie sich an, um zu kommentieren.

### Kategorien

Find more on Data Distribution Plots in Help Center and File Exchange

R2020b

### Community Treasure Hunt

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

Start Hunting!

Translated by