Creating specific grid lines for heatmap

93 views (last 30 days)
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 Comment
Adam Danz
Adam Danz on 16 Mar 2021
The problem with changing the xtick is that your tick labels will also be affected.

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 16 Mar 2021
Edited: Adam Danz on 17 Mar 2021
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
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 Comments
George Tomou
George Tomou on 17 Mar 2021
Edited: George Tomou on 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!

Sign in to comment.

More Answers (0)

Categories

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

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!