How to change heatmap data labels
53 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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');
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

