Azzera filtri
Azzera filtri

How to remove the Range from colorbar in HeatMap

19 visualizzazioni (ultimi 30 giorni)
Picture 1.JPG
Hello to all,
I wanted to place in the legend only the number 0 (red), 1 to yellow and 2 to green, I dont want to range from 0 to 2. It is possible to have a square red to 0, a square yellow to 1, and a square green to 2, like the NaN square?

Risposta accettata

Adam Danz
Adam Danz il 28 Gen 2020
Modificato: Adam Danz il 28 Gen 2020
Heatmaps are notoriously difficult to customize outside of the HeatmapChart Properties. The only property that I'm aware of that affects the heapmap colorbar is the ColorbarVisible property that just turns the colorbar on/off and resizes the heatmap accordingly.
One alternative is to use imagesc() instead which gives you full access to the colorbar and many other properties.
If you'd like to stick with a heatmap, here's a workaround that puts a customizable colorbar on top of the existing heatmap colorbar.
% Create the heatmap with a heatmap-colorbar
fh = figure(); % We'll use the figure handle.
data = randi(3,8,8)-1;
data(logical(eye(8))) = NaN;
hm = heatmap(data); % We'll use the heatmap output object.
colormap([1 0 0; 1 1 0; 0 1 0])
% Put an axis over top of the heatmap
% The axis will not be visible but will cover all area
% to the right of the heatmap.
hmp = hm.Position;
cbax = axes('Position',[sum(hmp([1,3])), 0, 1-sum(hmp([1,3])), 1],...
'XTick',[], 'YTick', [], 'Color',fh.Color);
cbax.XAxis.Visible = 'off';
cbax.YAxis.Visible = 'off';
% Set the new axis color map to match the
% heatmap's colormap
cbax.Colormap = hm.Colormap;
% Add a colorbar the same vertical position as the heatmap
cbh = colorbar(cbax,'Position',[.90, hmp(2), .03, hmp(4)]);
% Set the limits to 0:1 and set the ticks so that they
% are in the center of each bar
cbh.Limits = [0,1];
nColors = size(hm.Colormap,1);
cbh.Ticks = (1/nColors : 1/nColors : 1) - 1/nColors/2;
% Set the new labels for each tick
cbh.TickLabels = 0:nColors-1;
% Set the colorbar fontsize to the same value as heatmap fontsize
cbh.FontSize = hm.FontSize;
Note that the NaN values are no longer represented in the new colorbar.

Più risposte (0)


Scopri di più su Colormaps 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