Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do definitely associate colors to values in a heatmap in Matlab?

I have a matrix M of integers (1, 2 or 3). I'd like to represent it with a heatmap and associate a fixed color to 1, 2 and 3. I use this piece of code :

map = [1, 1, 0;  % color for 1 (yellow)
       1, 0.5, 0 ; % color for 2 (orange)
       0, 1, 0.5]; % color for 3 (green)

HeatMap(M,'Colormap',map,'Symmetric','false'); 

When M contains at least one 1, one 2 and one 3, there isn't any problem. But when M contains only 3s for example, the heatmap isn't as I want (all green). How can I solve this problem?

like image 278
JuliaR Avatar asked Mar 20 '15 10:03

JuliaR


1 Answers

It doesn't look like you can do this easily. In Matlab 2013b or older (I haven't tried in 2014b) when you call HeatMap it internally goes through a process of creating axes and settin the colors and so on. Eventually it gets to a point inside plot.m where the following function is called:

function scaleHeatMap(hHMAxes, obj)
%SCALEHEATMAP Update the CLIM in image axes
if obj.Symmetric
    maxval = min(max(abs(obj.Data(:))), obj.DisplayRange);
    minval = -maxval;
else
    maxval = min(max(obj.Data(:)), obj.DisplayRange);
    minval = min(obj.Data(:));
end
set(hHMAxes, 'Clim', [minval,maxval]);
end

This function does actually define the limits of the colormap using the axes of the heatmap (hHMAxes), but that object is not returned by the HeatMap() call, unfortunately.

The only ways I can think of getting out of this problem are:

  1. Modify the plot.m function. This is generally a quite bad idea.
  2. Create a myHeatMap function, that does everything the original one does but with a changed functionality on the Clim property on the axes.
  3. Do not use HeatMap at all and create an equally looking plot using e.g. surf or imshow.
  4. Do an if statement before the call of HeatMap, and check if there is a single value in the colormap (numel(unique(M(:)))==1), and if that happens, change your map to a single valued colormap, with the color of your choice.

The easiest one is by far 4.

like image 143
Ander Biguri Avatar answered Sep 27 '22 23:09

Ander Biguri