Creating specific grid lines for heatmap

33 ビュー (過去 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 Center および File ExchangeData Distribution Plots についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by