How to plot two contour filled plots of two different datasets using different colormap and caxis range in the same axis?
Show older comments
Dear all,
I want to plot two contour filled plots of two different variables with different colormaps and caxis ranges in the same axis. I tried with 'hold on' to plot two contour filled plots in the same axis, but it didn't work. Maybe it doesn't work with two contour filled plots?? I have gone through a previous conversation here (https://www.matlab.com/matlabcentral/answers/827960-setting-color-scale-of-two-datasets-in-the-same-axis-in-matlab), and modfied the code as per my case. But, I had no luck. I want the kind of figure like the figure shown in Ding et al. (2020).
I am attaching this figure as a sample for your convenience.
Can anyone please help me to solve the issue? That will be really helpful for me. My modifed MATLAB code is given below:
figure(1)
% one axes
contourf(X,Y,A,100,'linecolor','none')
cb1 = colorbar;
caxis([-2 2])
xlim([20 26])
ylim([100 1000])
h1 = gca;
h2 = axes;
% the other axes
contourf(X,Y,B,100,'linecolor','none')
% set(h2,'Color','none')
caxis([-0.25 0.25])
xlim([20 26])
ylim([100 1000])
cb2 = colorbar;
box on
set(h1,'yscale','log','YDir','reverse','Position', [0.45,0.48,0.26,0.42],'YTick','','YTickLabel','','XTick','','XTickLabel','',...
'FontName', 'Arial','FontSize',18,'FontWeight','bold','LineWidth', 1.5)
set(h2,'yscale','log','YDir','reverse','Position', [0.45,0.48,0.26,0.42],'YTick','','YTickLabel','','XTick','','XTickLabel','',...
'FontName', 'Arial','FontSize',18,'FontWeight','bold','LineWidth', 1.5)
% store the position and limits because adjusting the colorbar will mess these up
axpos = get(h1,'position');
xl = get(h1,'xlim');
yl = get(h1,'ylim');
set(cb1,'Position',[0.718,0.483,0.007,0.199],'YTick',-2:0.5:2,'FontName', 'Arial','FontSize',10.5,'FontWeight','bold','LineWidth', 1.5)
set(cb2,'Position',[0.718,0.698,0.007,0.199],'YTick',-0.25:0.1:0.25,'FontName', 'Arial','FontSize',10.5,'FontWeight','bold','LineWidth', 1.5)
% reassert axes geometry so they match
set(h1,'position',axpos,'xlim',xl,'ylim',yl);
set(h2,'position',axpos,'color','none','xlim',xl,'ylim',yl);
% set one colormap for each axes
cmap1 =jet(40); % Specify the desired number of colors
colormap(h1,cmap1)
cmap2 =parula(60); % Specify the desired number of colors
colormap(h2,cmap2)
4 Comments
Dyuman Joshi
on 20 Nov 2023
"I tried with 'hold on' to plot two contour filled plots in the same axis, but it didn't work."
But does "it didn't work" mean in this context?
It would help to have your data, so that we can see what the results from your code are and what modifications/changes your code needs.
ANKAN SARKAR
on 20 Nov 2023
DGM
on 20 Nov 2023
I'm a bit distracted at the moment, but as I'm sure you've found out by now, you can't do this in a single parent axes (or at least not reasonably). As in the linked thread, you need to use two overlaid axes and you'd then also need to manipulate the transparency of the upper contour() object accordingly.
Manipulating the contour transparency is a bit of an obstacle, depending on the version.
Contour objects had a scalar FaceAlpha property added in R2022b, so that doesn't help you either way. The first two options may be useful.
There may be other simpler options available via strategic selection of the LevelList property of the contour object. Having relevant example data would help.
ANKAN SARKAR
on 20 Nov 2023
Accepted Answer
More Answers (0)
Categories
Find more on Graphics Object Properties in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
