Search code examples
matlabmatrixheatmapcolor-mappingcolormap

Creating a heatmap/colormap in Matlab, by mixing two colors


Hey I have two big matrices 400x400. That I want to create into one heatmap/colormap. Current code:

res = matrix_1*256/2 + matrix_2*256/2;
%res = res -max(max(res));
HeatMap(res)
surf(res,'EdgeColor','none');
view(0,90);
colormap(gray);
colorbar
disp('done');

where the heatmap function everyone can look up. But to give a visualization of the second one it results in: enter image description here

This however does not let me know which matrix is dominant. But only that both are dominant (white) both are not dominant (dark). I would like to make a plot where I use fused data. E.g. Matrix 1 is nuance of red and Matrix 2 is nuance of green:

rgb = [matrix_1(i,ii), matrix_2(i,ii), 0]

then I want to make a 2D plot using the color that rgb represents. Is this possible ? I have looked at making my own colormap (but you guessed with no good results).

I have found solutions like this (how-to-create-an-interpolated-colormap-or-color-palette-from-two-colors) and create-a-colormap-in-matlab, but I how do I specify a specific colour for every point in a 2D plot?


Solution

  • Like this:

    RGB = cat(3, matrix_1, matrix_2, zeros(size(matrix_1)));
    imshow(RGB)
    

    Now the chart will be black were neither are dominant, red where matrix_1 is but matrix_2 is not, green where matrix_2 is but matrix_1 is not and yellow where they both dominate.

    If you wanted, you could even convert this back to an indexed image and get the colormap that colours it this way using rgb2ind and then create a surface plot using your original res for the heights (note there is no longer a need to scale this) and your new indexed image (ind) to specify the colours which are no longer enitrely goverened by height

    res = (matrix_1 + matrix_2)/2;
    [ind, map] = rgb2ind(RGB);
    surf(res, ind, 'EdgeColor','none');
    colormap(map)
    colorbar