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

339 views (last 30 days)
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
  1 Comment
Martin
Martin on 6 May 2025
I have personally found it much easier to just append 2 colormaps to create a combined colormap then scale your data to utilise the different regions of the appended colormap.
Eg:
%Create the combined colormap!
x = 1:2:1000;
s=jet(1000);
p=cool(1000);
m = [s(x,:);p(x,:)];
%Get some points
[X,Y] = meshgrid([0:0.1:1],[0:0.1:1] );
figure
%use the combined map
colormap(m);
%plot from 0 to 1 using the cool map
imagesc([0:0.1:1],[0:0.1:1],Y,[-1 1])
hold on
%Scale the other data between 0 and 500
s= surf(X,Y,0.4*ones(11),X*500);
s.CDataMapping = 'direct';
campos([1 1 0.5]);

Sign in to comment.

Accepted Answer

Mike Garrity
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.
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.
  16 Comments
Serena
Serena on 4 Mar 2025
I am having trouble getting two different colourmaps to work within subplots. That is each subplot has two colourmaps. I can get it to work for a figure containing one 3D plot with two surfaces using different colourmaps, but I can't get the same thing to work when making subplots (to compare two different simulations side by side). I tried exporting the figures as different types of png, tiffn etc with higher resolution then using imread to make subplots, but the resulting figure has extremely low resolution (the axis labels etc are very poorly reproduced), no matter what settings I used when exporting the figures.
I tried importing the figures, but both colorbars where not read.
Do you know how I can use the axes command to save two different axes which I can then set two different colorbars to, when making subplots?
Thank you in advance.
Adam Danz
Adam Danz on 5 Mar 2025
Sometimes using axes overlays is problematic. For example, plotting the two toroid surfaces below is not possible if they are in different axes. If it's not clear why, see this animation of the axes rotating.
Instead of using axes overlays, use the CData property to specify TrueColor arrays. For more info and a demo see my full answer in another thread or Voss' answer using a slightly different approach.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!