Creating specific grid lines for heatmap

조회 수: 29 (최근 30일)
George Tomou
George Tomou 2021년 3월 16일
편집: Adam Danz 2025년 4월 9일
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
Adam Danz
Adam Danz 2021년 3월 16일
The problem with changing the xtick is that your tick labels will also be affected.

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2021년 3월 16일
편집: Adam Danz 2025년 4월 9일
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
George Tomou
George Tomou 2021년 3월 17일
편집: George Tomou 2021년 3월 17일
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 2021년 3월 17일
👍

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by