MATLAB: Using imagesc to plot two matrices on top of each other with different colormaps

imagescMATLABmultiple colormapsmultiple plotsplotting

I have two images:
orgImage, which is a 1298×320 matrix of values that I wish to show in gray scale.
And
ChemMap, which is a 1298×320 matrix where a majority of the elements are NaNs, but some of which are values that I want to plot on top of orgImage with a colormap such as jet for example.
Plotting these on top of each other should be the simplest thing in the world but I have literally been trying for hours to get it the way I want it to but it doesn’t work.
First I was hoping to be able to start by plotting image number one like:
imagesc(orgImage), colormap('gray'),axis equal, hold on
and then add the other one on top;
imagesc(ChemMap), colormap('jet')
But that doesn’t work since the NaN’s are still plotted on the image but as the lowest possible value of the jet colormap.
I’ve tried plotting the images on different axis as suggested here: https://se.mathworks.com/matlabcentral/answers/101346-how-do-i-use-multiple-colormaps-in-a-single-figure
and I’ve tried messing around with the freezeColors routine on fileexchange but I can’t quite get it to work which is overwhelmingly frustrating.
Does anyone know how I can make the NaN’s transparent in imagesc(ChemMap) so I can plot these images on top of each other?
Thanks.

Best Answer

This is a bit tricky, because you need two colormaps combined in a single colormap and scale the values in one image to match the second colormap. The code below works if you have scaled the values in your images to the range 1,256.
Fake some data:
Ncolors = 256;
orgImage = randi(Ncolors, 10);
ChemMap = randi(Ncolors, 10); ChemMap(ChemMap > 128) = NaN; ChemMap = 2*ChemMap;
Define your color map
cmap = [gray(Ncolors); jet(Ncolors)];
colormap(cmap)
Combine both images and scale the values to the right indices in the new colormap
I = Ncolors + ChemMap;
ind = isnan(I);
I(ind) = orgImage(ind);
imagesc(I)