Creating specific grid lines for heatmap

Hi all,
I am creating a heatmap of correlations using the heatmap function. I have been able to toggle the grid property on and off, but am having difficulty trying specific grid lines where I need them. From what I've read, this is generally accomplished using XTick and YTick for graphing purposes, but when I try it here I get the following error:
Unrecognized property 'XTick' for class 'matlab.graphics.chart.HeatmapChart'.
What I am trying to do, for example, is create easily recognizable groups of hot spots. I've attached below the code and some figures (same data with either grid on or grid off) to try to illustrate the goal. Let's say I wanted horizontal and vertical grid lines after the 3rd row and 3rd column. How can I add these to my figure? Any assistance would be greatly appreciated!
HeatmapTest=[
0.0000 0.4230 0.5668 0.5079 0.4572
0.4230 0.0000 0.4343 0.4262 0.4703
0.5668 0.4343 0.0000 0.5535 0.5072
0.5079 0.4262 0.5535 0.0000 0.5099
0.4572 0.4703 0.5072 0.5099 0.0000];
labelsTest=categorical({'RH TO1/MT','RH vTO','RH LO1','RH V3A','RH hV4'});
figure(1)
fixMap=heatmap(FixheatmapTest2);
fixMap.Title='Fixation Community Structure';
fixMap.XData=labelsTest;
fixMap.YData=labelsTest;
fixMap.Colormap=jet;
% grid(fixMap, 'off')
caxis([0 1]);
figure(2)
fixMap=heatmap(FixheatmapTest2);
fixMap.Title='Fixation Community Structure';
fixMap.XData=labelsTest;
fixMap.YData=labelsTest;
fixMap.Colormap=jet;
grid(fixMap, 'off')
caxis([0 1]);

1 commentaire

Adam Danz
Adam Danz le 16 Mar 2021
The problem with changing the xtick is that your tick labels will also be affected.

Connectez-vous pour commenter.

 Réponse acceptée

Adam Danz
Adam Danz le 16 Mar 2021
Modifié(e) : Adam Danz le 9 Avr 2025
Heatmaps are difficult to customized since many of their handles and properties are hidden or undocumented. imagesc is a good alternative (see example).
If you want to stick with the heatmap, to customize the grid lines you'll need to use undocumented methods which are liable to change in future release.
Tested in r2020b and r2021a
hm = heatmap(magic(9));
% Get underlying axis handle
origState = warning('query', 'MATLAB:structOnObject');
cleanup = onCleanup(@()warning(origState));
warning('off','MATLAB:structOnObject')
S = struct(hm); % Undocumented
ax = S.Axes; % Undocumented
clear('cleanup')
% Remove grids
grid(hm,'off'); % alternative: hm.GridVisible = 'off';
% Place lines around selected columns and row
% Assumes columns and rows are 1 unit in size!
col = [5, 8];
row = [1, 5, 9];
xline(ax, [col-.5, col+.5], 'k-'); % see footnotes [1,2]
yline(ax, [row-.5, row+.5], 'k-'); % see footnotes [1,2]
Footnotes
[1] For Matlab release prior to r2021a, use
arrayfun(@(x)xline(ax,x,'k-'),[col-.5, col+.5]);
arrayfun(@(x)yline(ax,x,'k-'),[row-.5, row+.5]);
[2] You may want to make the reference lines wider using
xline(ax, [col-.5, col+.5], 'k-', 'LineWidth',2)
yline(ax, [row-.5, row+.5], 'k-', 'LineWidth',2)
or fully opaque
xline(ax, [col-.5, col+.5], 'k-', 'Alpha', 1)
yline(ax, [row-.5, row+.5], 'k-', 'Alpha', 1)

5 commentaires

George Tomou
George Tomou le 17 Mar 2021
Modifié(e) : George Tomou le 17 Mar 2021
I should have mentioned in the original post, but I am using r2020b.
I've tried running your code and receive the follwing:
hm.GridVisible = 'off';
Invalid or deleted object.
Additionally, I tried to incorporate the code into my figure. While the S=struct(hm) from your example works just fine, when I use my data I get this:
S = struct(fixMap);
Error using struct
Invalid or deleted object.
Finally, would the -.5 and +.5 manual calculations work for the categorical data I use in the original post? Could it be that the reason I am getting the above error is also because the heatmap is dealing with categorical input?
Adam Danz
Adam Danz le 17 Mar 2021
Modifié(e) : Adam Danz le 17 Mar 2021
> I should have mentioned in the original post, but I am using r2020b.
My solution works and has been tested in r2020b and r2021a as is indicated in the answer. For r2020b you'll need to use footnote [1].
Both of the error messages you shared clearly indicate what the problem is. The objects hm and fixMap were deleted. They no longer exist and aren't defined. Your implementation is different from the example in the answer.
Adam Danz
Adam Danz le 17 Mar 2021
Modifié(e) : Adam Danz le 17 Mar 2021
> would the -.5 and +.5 manual calculations work for the categorical data?
The labels are categorical but the heatmap values and the underlying axes are numeric so yes, it will work.
col = 3;
row = 3;
This uses line widths of 5
George Tomou
George Tomou le 17 Mar 2021
Modifié(e) : George Tomou le 17 Mar 2021
That worked! Thanks so much!
--If I'm not mistaken, the values for 'row' and 'col' tell matlab where to draw the line. Which variable is the line thickness?--
Found it, using 'LineWidth' in the arrayfun line. Thanks again!
Adam Danz
Adam Danz le 17 Mar 2021
👍

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Distribution Plots dans Centre d'aide 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