Use two different color schemes in contour plot

3 ビュー (過去 30 日間)
Eric Roden
Eric Roden 2022 年 6 月 27 日
編集済み: dpb 2022 年 6 月 28 日
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 件のコメント
dpb 2022 年 6 月 27 日
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?


回答 (3 件)

Walter Roberson
Walter Roberson 2022 年 6 月 27 日
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 件のコメント
dpb 2022 年 6 月 28 日
We'd need the data to do anything useful, Attach a .mat file with the variables.


Eric Roden
Eric Roden 2022 年 6 月 28 日
Yes, agreed, understood. I have added the text files that generate the plots to a Google Drive folder that can be accessed at the link below, and pasted in below a modified script that would generate the overlayed contour plots. I specified the "winter" colormap for both plots (when/if they can both be made able to show up), as this will reduce any weirdness in terms of color mixing and at the same time should show two "peaks" in high values, one in the upper 1 m of the water column, and one starting at a depth of around 2 m. The key thing is to get both contour plots to show in the same figure, which is still not happening despite my inclusion of the freezeColors command. Thanks in advance for any additional help you can provide.
%% Overlay contour plot of Cyano and Chlorob concentrations
fh = figure();
%fh.WindowState = 'maximized';
set(0,'DefaultAxesFontSize',18, 'defaultlinelinewidth', 2,'DefaultAxesTitleFontWeight', 'normal')
hold on
%title('Biomass (\mumol C L^{-1})')
pbaspect([asp1 asp2 asp3])
xlim([139 229])
set(gca,'xtick',[139 169 199 229])
ylim([-0.001 7])
ylabel({'Depth (m)'});
xlabel({'Julian Date'});

dpb 2022 年 6 月 28 日
編集済み: dpb 2022 年 6 月 28 日
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
[~,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
pos=hAx.Position; % the present axes position
pos(3)=0.85*pos(3); % shrink it some for some more room knowing second colorbar
hold on
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...
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 件のコメント
dpb 2022 年 6 月 28 日
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
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.





Community Treasure Hunt

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

Start Hunting!

Translated by