Use two different color schemes in contour plot

73 ビュー (過去 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
dpb 2022 年 6 月 27 日
編集済み: 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 件のコメント
Eric Roden
Eric Roden 2022 年 6 月 28 日
編集済み: dpb 2022 年 6 月 28 日
Dear dpb and Walter, thanks very much for your continued thinking and input about this. I downloaded freezeColors from File Exchange, and tried to implement in my code. I have pasted in the code below. Without trying to explain the details of what's being plotted in great detail, these are modeled distributions in space (depth) and time (julian date) of photosynthetic microorganisms that reside at different places with a lake water column, with the Cyanos above the Chlorob throught the modeled period. Although there will be some minor overlap between the distributions of these two groups, I believe they are sufficiently separated such that a contour plot that includes both with different color schemes won't look too bad. Although the freezeColors code excutes in the code below, I still only see the second group, i.e. the Chlorob, in the plot that is generated. I'm probably missing something simple to make this work, but am not sure what. Thanks for having a look as time permits. And again, thanks a million for thinking through this on my behalf. Eric Roden, Department of Geoscience, University of Wisconsin-Madison.
%% Overlay contour plot of Cyano and Chlorob concentrations
fh = figure();
%fh.WindowState = 'maximized';
set(0,'DefaultAxesFontSize',18, 'defaultlinelinewidth', 2,'DefaultAxesTitleFontWeight', 'normal')
asp1=4;
asp2=2;
asp3=1;
hold on
contour(t+139,x,Cyano,'showtext','off','fill','on','linewidth',2);
colormap(YlOrBr)
freezeColors
contour(t+139,x,Chlorob,'showtext','off','fill','on','linewidth',2);
colormap(YlGn)
%title('Biomass (\mumol C L^{-1})')
pbaspect([asp1 asp2 asp3])
xlim([139 229])
set(gca,'xtick',[139 169 199 229])
ylim([-0.001 7])
set(gca,'ydir','reverse')
ylabel({'Depth (m)'});
xlabel({'Julian Date'});
colorbar
dpb
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
loadfilename=fullfile('tout.txt');
t_out=load(loadfilename);
loadfilename=fullfile('Cyano.txt');
Cyano_out=load(loadfilename);
loadfilename=fullfile('Chlorob.txt');
Chlorob_out=load(loadfilename);
nx=700;
lx=70;
dx=lx/nx;
x=(dx:dx:lx)/10;
fh = figure();
%fh.WindowState = 'maximized';
set(0,'DefaultAxesFontSize',18, 'defaultlinelinewidth', 2,'DefaultAxesTitleFontWeight', 'normal')
asp1=4;
asp2=2;
asp3=1;
hold on
contour(t_out+139,x,Cyano_out,'showtext','off','fill','on','linewidth',2);
%colormap(YlOrBr)
colormap(winter)
freezeColors
contour(t_out+139,x,Chlorob_out,'showtext','off','fill','on','linewidth',2);
%colormap(YlGn)
colormap(winter)
%title('Biomass (\mumol C L^{-1})')
pbaspect([asp1 asp2 asp3])
xlim([139 229])
set(gca,'xtick',[139 169 199 229])
ylim([-0.001 7])
set(gca,'ydir','reverse')
ylabel({'Depth (m)'});
xlabel({'Julian Date'});
colorbar

dpb
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
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 件のコメント
Eric Roden
Eric Roden 2022 年 6 月 28 日
Dear dpg: Thanks for sending this. Just quickly though, I just grabbed your code and ran it, and produced the the attached...only one of the contours appears. Maybe it has something to do with the version of the software (R2020b) that I'm using?
dpb
dpb 2022 年 6 月 28 日
編集済み: 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
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeContour Plots についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by