How to remove the Range from colorbar in HeatMap

조회 수: 25 (최근 30일)
Tiago Dias
Tiago Dias 2020년 1월 28일
댓글: Tiago Dias 2020년 1월 29일
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?
Thanks!

채택된 답변

Adam Danz
Adam Danz 2020년 1월 28일
편집: Adam Danz 2020년 1월 28일
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.
clf()
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Colormaps에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by