![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/175608/image.png)
How can I use (and display) two different colormaps on the same figure?
691 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
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')
%%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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/175608/image.png)
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.
13 Comments
Yiqian Qian
on 5 Apr 2021
Does this work for a 3D plot? Basically, I want to overlay these two 3D plots which have their own colormap. So after merging, it should have a look like in this answer but with a 3D view. Thanks!
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/572877/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/572882/image.png)
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:
load([idDataDir,filesep,'Acond.mat']); % load skymap condition number
[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
% Link two axes together
hLink = linkprop([ax1,ax2],{'XLim','YLim','ZLim','CameraUpVector','CameraPosition','CameraTarget'});
% 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')
setappdata(gcf,'StoreTheLink',hLink); % store the link so that they can rotate and zoom synchronically
Code above finally produces a combined, synchronized 3D view of surf and scatter3 plot.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/572917/image.png)
More Answers (1)
Dimani4
on 24 May 2021
It works with separate figure but it doesnt in gui. see my thread (https://www.mathworks.com/matlabcentral/answers/837288-overlay-two-images-in-gui). maybe somebody can help me.
0 Comments
See Also
Categories
Find more on Purple in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!