MATLAB Answers

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

1,234 ビュー (過去 30 日間)
Nicholas 2015 年 3 月 26 日
編集済み: Hauke Kolster 2021 年 2 月 25 日 17:58
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 件のコメント



Mike Garrity
Mike Garrity 2015 年 3 月 26 日
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;
ax2 = axes;
%%Link them together
%%Hide the top axes
ax2.Visible = 'off';
ax2.XTick = [];
ax2.YTick = [];
%%Give each one its own colormap
%%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.

  11 件のコメント

表示 8 件の古いコメント
Daniel Holstein
Daniel Holstein 2019 年 11 月 22 日
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');
Elizabeth Ramsey
Elizabeth Ramsey 2020 年 10 月 1 日
I got this working well, but am now I'm trying to repeat it with a series of subplots, is this possible? I've had no success.
Hauke Kolster
Hauke Kolster 2021 年 2 月 25 日 17:56
Example 2: Use a Different Colormap in Two Separate Axes
If you are using MATLAB R2014b or later release, the function "colormap" accepts the handle to axes as input and allows you to set different colormaps for each axes. For example the following code snippet will plot two subplots with different colormaps.
ax1 = subplot(2,1,1);
ax2 = subplot(2,1,2);


その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by