MATLAB Answers

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

906 views (last 30 days)
Nicholas
Nicholas on 26 Mar 2015
Commented: Daniel Holstein on 22 Nov 2019
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

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.

  9 Comments

Show 6 older comments
Matthias Pospiech
Matthias Pospiech on 18 Jul 2017
The code works for me mostly, however the pcolor and contour plot I create do not overlap in size. the position set for both axis is however the same.
Code used:
figure_width = 14;
figure_height = 10;
% --- setup plot windows
figuresVisible = 'on'; % 'off' for non displayed plots (will still be exported)
hfig = figure(1); clf;
set(hfig,'Visible', figuresVisible)
set(hfig, 'units', 'centimeters', 'pos', [5 5 figure_width figure_height])
set(hfig, 'PaperPositionMode', 'auto');
set(hfig, 'Renderer','opengl');
set(hfig, 'Color', [1 1 1]); % Sets figure background
set(gca, 'Color', [1 1 1]); % Sets axes background
% --- dimensions and position of plot
hsp = subplot(1,1,1, 'Parent', hfig);
set(hsp,'Position',[0.15 0.17 0.75 0.80]);
ax1 = axes;
%%plot
colorDepth = 1000;
hold on;
% 3D flat plot
caxis([0 100]);
cmap = fliplr(jet(colorDepth));
colormap(ax1, cmap(1:800,:));
hplot = pcolor(ax1, xAxis, yAxis, Data2D);
shading flat; % do not interpolate pixels
axis image
set(gca,'XLim',[xAxis(1) xAxis(end)]);
% colorbar
hcb = colorbar('location','EastOutside', ...
'FontSize' , FontSize , ...
'FontName' , FontName);
set(hcb, 'Ylim', [0 100]);
set(hcb, ...
'Box' , 'on' , ...
'TickDir' , 'in' , ...
'TickLength' , [.02 ] , ...
'LineWidth' , 0.6);
% contour plot
ax2 = axes;
colormap(ax2, flipud(gray(colorDepth)));
[C,hfigc] = contour(ax2, xAxis, yAxis, DataMean2D,zlevs);
%%Link them together
linkaxes([ax1,ax2])
%%Hide the top axes
ax2.Visible = 'off';
ax2.XTick = [];
ax2.YTick = [];
set(hfigc, 'LineWidth',1.0);
set(hfigc, ...
'ShowText','on', ...
'TextStep',get(hfigc,'LevelStep')*2 );
hcollab = clabel(C,hfigc,'LabelSpacing',350); %
set(hcollab, ...
'BackgroundColor',[.99 .99 .99], ...
'Edgecolor',[.1 .1 .1], ...
'FontSize',FontSize - 5, ...
'FontName', FontName, ...
'Color',[0 0 0], ...
'Rotation',0, ...
'HorizontalAlignment','center', ...
'Margin',2)
hold off;
setup axis plot properties
set([ax1, ax2], ...
'Box' , 'on' , ...
'TickDir' , 'in' , ...
'TickLength' , [.02 .02] , ...
'XMinorTick' , 'off' , ...
'YMinorTick' , 'off' , ...
'XGrid' , 'off' , ...
'YGrid' , 'off' , ...
'XColor' , [.0 .0 .0], ...
'YColor' , [.0 .0 .0], ...
'LineWidth' , 0.6 );
% bring axis on top again (fix matlab bug)
set([ax1,ax2],'Layer', 'top');
%%Then add colorbars and get everything lined up
set([ax1,ax2],'Position',[.17 .11 .685 .815]);
Anne
Anne on 16 Aug 2018
I also get this problem with different sizes. Did you find a solution to this problem?
Daniel Holstein
Daniel Holstein on 22 Nov 2019
The trick to getting them to line up is to extract the axis properties from the first axis. Here's an example.
P = get(ax1,'Position');
XLIM = get(ax1,'XLim');
YLIM = get(ax1,'YLim');
PA = get(ax1,'PlotBoxAspectRatio');
set(ax2,'Position',P,'XLim',XLIM,'YLim',YLIM,'PlotBoxAspectRatio',PA)

Sign in to comment.

More Answers (0)