# Use two different color schemes in contour plot

3 views (last 30 days)

Show older comments

##### 1 Comment

dpb
on 27 Jun 2022

Well, I can see why...I can't visualize how you would put those two together on one plot, either.

Can you sketch by hand a representation of what you visualize this would look like?

Or perhaps you have a reference from a paper/text that you're trying to duplicate?

### Answers (3)

Walter Roberson
on 27 Jun 2022

You will need to use two different axes, as only one colormap can be active per axes. The two axes can be in the same position.

Alternatively, you can plot one, and then use File Exchange freezecolors() to lock it in to rgb, after which you plot the other with a different colour map. You would, however, need to draw the second colorbar yourself.

##### 7 Comments

dpb
on 28 Jun 2022

Edited: dpb
on 28 Jun 2022

OK, with the explanation that you expect the two contours to not actually overlap, then it's feasible -- here's a sample with an arbitrary contour from the doc example to illustrate the two-axes approach.

[X,Y,Z] = peaks; % our data

hAx=axes;

[~,hC]=contour(X-3,Y,Z,20); % put first contour on it; keep object handle

xlim([-6 6]) % have to reset the limit; could linkaxes???

colormap(hAx,"cool") % and arbitrary colormap

pos=hAx.Position; % the present axes position

hCB=colorbar(hAx);

pos=hAx.Position; % the present axes position

pos(3)=0.85*pos(3); % shrink it some for some more room knowing second colorbar

hAx.Position=pos;

hold on

hAx(2)=axes('position',hAx(1).Position,'color','none',"XTick",[],'YTick',[]);

linkaxes(hAx,'x') % This will tie the two axes x axis limits/sizes together

hold(hAx(2),'on') % HERE'S the key piece I inadvertently left out before...

[~,hC(2)]=contour(hAx(2),X+3,Y,Z,20); % SO, the 2nd verse on contour w/o HOLD ON reset the axes...

colormap(hAx(2),"hot") % different colormap this axes

hCB(2)=colorbar(hAx(2)); % and the other colorbar to match

posCB1=hCB(1).Position; % get their positions to adjust...

posCB2=posCB1;

posCB2(1)=posCB1(1)+2*posCB1(3)+0.015;

hCB(2).Position=posCB2;

produces

It appears could have done the resizing of the axes later, maybe to not shrink as much; I didn't play with refinements to try to optimize the use of real estate.

But, there are two contours in what appears to be the same space sharing axes and each with its own colormap/colorbar.

Salt to suit... :)

##### 2 Comments

dpb
on 28 Jun 2022

Sorry about that..I had done just interactively at command line with several retracts/restarts and then pasted the sequence from the commandhistory window and subsequently tried to pick/choose the correct sequence and remove the missteps.

The key one I missed keeping is the hold on for the second axes after it is created with the 'Color','none' parameter -- the high level plotting commands like contour automagically reset a bunch of stuff into a new axes including the default color -- which when it gets set to [1 1 1] is opaque and so occludes the bottom layer of the first axes showing through. It also resets ticks and all which is what screwed up the legends.

If you will execute

hAx(2).Color='none';

while you have that previous figure as the current figure, you'll see the other contour/axis content show up -- it is there, just occluded. This is a key point to get in doing these kinds of machinations with HG2 which one ends up doing way too much of with MATLAB.

I fixed up the script above making just a couple of minor refinements along the way, but it did produce the desired end result when I did run the actual code above.

NB: The above doesn't fiddle with the 2nd axes y range nor move the YAxisPosition to the RHS side; you may want to do that, your choice to show other ticks/tick values on right, too, or not.

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!