How to change heatmap data labels

53 visualizzazioni (ultimi 30 giorni)
Hal Owens
Hal Owens il 24 Mag 2021
Modificato: Adam Danz il 24 Nov 2023
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.

Risposta accettata

Adam Danz
Adam Danz il 24 Mag 2021
Modificato: Adam Danz il 24 Nov 2023
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 Commento
Hal Owens
Hal Owens il 24 Mag 2021
Appreciate the help this should work for my use case.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Data Distribution Plots in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by