How to change heatmap data labels

54 ビュー (過去 30 日間)
Hal Owens
Hal Owens 2021 年 5 月 24 日
編集済み: Adam Danz 2023 年 11 月 24 日
I am trying to use heatmap() to generate a heatmap like this:
But I need to be able to change the values that are used in each cell. Instead of using the raw data value I would like to change each cell to have a label. I don't see any ovbious way to do this using the figure properties so I am wondering if this is possible.

採用された回答

Adam Danz
Adam Danz 2021 年 5 月 24 日
編集済み: Adam Danz 2023 年 11 月 24 日
Modifications to heatmap are not easy. I suggest recreating the heatmap using imagesc which will look almost exactly the same but will not have some of the interactivity options.
Example:
Generate data
rng('default') % for reproducibility
data = magic(5);
Create heatmap
figure()
heatmap(data)
title('heatmap')
Create imagesc plot that looks like a heatmap
This uses the same input data, data.
fig = figure();
ax = axes(fig);
h = imagesc(ax, data);
set(ax,'XTick',1:5,'YTick',1:5)
title('imagesc')
ax.TickLength(1) = 0;
% Create heatmap's colormap
n=256;
cmap = [linspace(.9,0,n)', linspace(.9447,.447,n)', linspace(.9741,.741,n)'];
colormap(ax, cmap);
colorbar(ax)
hold on
% Set grid lines
arrayfun(@(x)xline(ax,x,'k-','Alpha',1),0.5:1:5.5)
arrayfun(@(y)yline(ax,y,'k-','Alpha',1),0.5:1:5.5)
% Starting in R2021, xline and yline accept object arrays
% and you can replace the two lines above with these two lines
% xline(ax,ax.XTick+0.5,'k-','Alpha',1)
% yline(ax,ax.YTick+0.5,'k-','Alpha',1)
Specify text labels
Lables here are random characters
asc = [65:90, 97:122];
nLabels = 25;
labels = mat2cell(char(asc(randi(numel(asc), nLabels, 4))),ones(nLabels,1),4);
[xTxt, yTxt] = ndgrid(1:5, 1:5);
th = text(xTxt(:), yTxt(:), labels(:), ...
'VerticalAlignment', 'middle','HorizontalAlignment','Center');
  1 件のコメント
Hal Owens
Hal Owens 2021 年 5 月 24 日
Appreciate the help this should work for my use case.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by