Use two different color schemes in contour plot
Afficher commentaires plus anciens
I want to create a contour plot that shows two different data sets (chemical concentrations vs. depth and time in a lake), each with it's own color scheme and colorbar. There must be a way but I have been unable to figure out how. Thanks!
1 commentaire
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?
Réponses (3)
Walter Roberson
le 27 Juin 2022
0 votes
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 commentaires
Eric Roden
le 27 Juin 2022
dpb
le 27 Juin 2022
That's what yyaxis does; creates two axes on top of each other...or you can simply do so by
hAx=axes;
hAx(2)=axes('position',hAx.Position','color','none');
as the most basic.
But, I still can't see how two countour plots on the same axes aren't going to clash violently with each other and not just make a mess...
Walter Roberson
le 28 Juin 2022
I am not sure at the moment whether you can have separate colormaps for the two axes of yyaxes.
dpb
le 28 Juin 2022
I'm not either, Walter, which is why I pointed out the old way of effecting the two axes overlaying each other.
I did build two contour plots on those two axes and assigned colorbar to each; one will have to do some more machinations to keep them from laying on top of each other by adjusting their positions to also not overlay and make room for two in the same figure space, but can be done.
But, the two on top of each other was, as expected, just a mess...not much value as a graphic.
ADDENDUM: Just tried same experiment with yyaxis -- doesn't seem to work to even display the two contours to be able to see both; the second occludes the first. There is only one axes; there are two YAxis NumericRulers for the two sides but colorbar won't accept them as a handle.
dpb
le 28 Juin 2022
"I still can't see how two countour plots on the same axes aren't going to clash violently with each other..."
That wasn't written as clear as could/should have been--intended to refer to the two axes option shown where the two overlay each other, not the yyaxis option that is, fundamentally, still only one axes.
Eric Roden
le 28 Juin 2022
Modifié(e) : dpb
le 28 Juin 2022
dpb
le 28 Juin 2022
We'd need the data to do anything useful, Attach a .mat file with the variables.
Eric Roden
le 28 Juin 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 commentaires
Eric Roden
le 28 Juin 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.
Catégories
En savoir plus sur Contour Plots dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!