# How can I use (and display) two different colormaps on the same figure?

1,231 views (last 30 days)
Nicholas on 26 Mar 2015
Commented: Yiqian Qian on 5 Apr 2021
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

Mike Garrity on 26 Mar 2015
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')
%%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.
Yiqian Qian on 5 Apr 2021
Finally achieved by changing the linkaxes to linkprop, successfully combined a 3D scatter plot with its own colormap with a surf plot also has its own colormap. Talk is cheap, show me the code:
[x,y,z] = sphere(300); % generate a 300 faces unit sphere
% convert sources coord. to spherical
[id_x,id_y,id_z] = sph2cart(idRA,idDec,1);
[sim_x,sim_y,sim_z] = sph2cart(simRA_nm,simDec_nm,1);
[matched_x,matched_y,matched_z] = sph2cart(matched_alpha,matched_dec,1);
figure
ax1 = axes;
surf(ax1,x,y,z,Acond,'EdgeColor','Interp','FaceColor','Interp');
axis equal
ax2 = axes;
scatter3(ax2,id_x,id_y,id_z,40,idSNR,'filled');
hold on
scatter3(ax2,sim_x,sim_y,sim_z,'o','MarkerEdgeColor','#888E8F')
scatter3(ax2,matched_x,matched_y,matched_z,[],matched_snr,'s');
% connect identified sources with matched true sources with great circle on
% sphere
center = zeros(1,3); % center of sphere
for src = 1:length(idRA)
[v] = GreatCircle(center,[id_x(src),id_y(src),id_z(src)],[matched_x(src),matched_y(src),matched_z(src)],1);
plot3(ax2,v(1,:),v(2,:),v(3,:),'m')
% pause
end
axis equal
% Hide the top axes
ax2.Visible = 'off';
ax2.XTick = [];
ax2.YTick = [];
% Give each one its colormap
colormap(ax1)
colormap(ax2,'spring')
% get everthin lined up
cb1 = colorbar(ax1,'Position',[0.1 0.1 0.05 0.815]); % four-elements vector to specify Position [left bottom width height]
cb2 = colorbar(ax2,'Position',[0.81 0.1 0.05 0.815]);
cb1.Label.String = 'Condition Number';
cb2.Label.String = 'SNR';
cb1.Label.FontSize = 14;
cb2.Label.FontSize = 14;
legend({'Grid','Identified Sources','True Sources','Matched True Source','Matched & Identi.'},'Location','southeast')
Code above finally produces a combined, synchronized 3D view of surf and scatter3 plot. 