MATLAB: How to use (and display) two different colormaps on the same figure

colormapm_contourm_mapm_pcolorMATLABmultiple colormapspcolorscatter

Hey all,
My issue is that I would like to present a contour style map utilising m_map functions (using m_pcolor with colormap 'jet') that is overlaid with some scattered data (using m_scatter with colormap 'cool'). These two colormaps would then be displayed either side of the plot for reference. To me this seems quite a simple task but I can't seem to get Matlab to do it.
Can anyone help me? I've searched through the online community answers and there seem to be some answers that sound like they are relevant but then turn out not to be.
Thanks, Nick

Best Answer

It depends. Are you trying to put the pcolor in the same axes or in different axes. Starting in R2014b, MATLAB has a separate colormap for each axes, so the second case becomes pretty easy.
If you've got the first case (where they're in the same axes) things aren't as simple. If you've got R2014b or R2015a, then you can create two axes and overlay them. It would look a bit like the following:
%%Create two axes
ax1 = axes;
[x,y,z] = peaks;
surf(ax1,x,y,z)
view(2)
ax2 = axes;
scatter(ax2,randn(1,120),randn(1,120),50,randn(1,120),'filled')
%%Link them together
linkaxes([ax1,ax2])
%%Hide the top axes
ax2.Visible = 'off';
ax2.XTick = [];
ax2.YTick = [];
%%Give each one its own colormap
colormap(ax1,'hot')
colormap(ax2,'cool')
%%Then add colorbars and get everything lined up
set([ax1,ax2],'Position',[.17 .11 .685 .815]);
cb1 = colorbar(ax1,'Position',[.05 .11 .0675 .815]);
cb2 = colorbar(ax2,'Position',[.88 .11 .0675 .815]);
As you can see, the messiest part of that is that you don't get automatic layout with two colorbars. That means that you need to position them yourself.
If you're using an earlier version of MATLAB, then things are a little harder. There are a couple of file exchange utilties for combining two colormaps and then offseting the CData of one of your objects. That's probably the approach you'd have to take.