Overlaying two contour plots on same axis

97 views (last 30 days)
James Wyatt
James Wyatt on 30 Mar 2020
Commented: Elie Hatem on 31 Aug 2020
I am trying to plots two contours on top of each other
Code is below, cannot work out why it is not working.
figure(1)
[c1,h1]=contourf(long1,lat1,squeeze(asal(:,:,36))',[34.6:0.01:34.9],'LineColor','none')
cmocean('balance')
colorbar
hold on
[c2,h2]=contour(long1,lat1,squeeze(pot_dens(:,:,36))',[26.80:.01:27.0],'k--')
clabel(c2,h2)
I have attached the different plots seperately, and the combined result.
I want just the sal plot as is, with the potential density contours overlaid
Thanks in advance
  3 Comments
James Wyatt
James Wyatt on 22 Jun 2020
Edited: darova on 22 Jun 2020
Hi Elie,
I have some code that works kind of well:
figure(1)
ax(1)=axes;
contourf(long1,lat1,squeeze(PV(:,:,34))',[0.05*10^-10:0.05*10^-10:1*10^-10],'LineColor','none')
cmocean('balance')
hc(1)=colorbar;
ax(2)=axes;
[c2,h2]=contour(long1,lat1,squeeze(dyn_height(:,:,34))',[10:.1:15],'c--')
clabel(c2,h2)
set(ax(2), 'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none');
pos=get(ax(1),'Position');
set(ax,'Position',[pos(1) pos(2) 0.9*pos(3) pos(4)]);
cpos=get(hc(1),'position');
set(hc(1),'position',[cpos(1)+0.05 cpos(2:4)]);
title('PV at 500m depth in SEI Ocean with dynamic height overlay')
xlabel('Longitude')
ylabel('latitude')
This does give an overlay, however I'm sure there is probably a simpler way of doing it.

Sign in to comment.

Answers (1)

Pratheek Punchathody
Pratheek Punchathody on 31 Aug 2020
Hi James,
As per my understanding you are looking for plotting two contour plots on the same axis.
Here is the reference code where two contour plots are overlaid on the same axis.
%%code starts here
Z1 = peaks; %Sample data 1
Z2 = peaks.^2; %Sample data 2
hold on
contourf(Z1,'-r');
contour(Z2,'--k');
colormap('map')
axis equal
%%code ends here
The plot for the above code with the sample data where the two counter plots are overlaid is shown below
The output figure shows both Z1 and Z2 are contour plotted on the same axis, with Z1 being filled 2D plot and Z2 with the contour plot.
Potential density contours are overlaid with the different colour using the "colormap"
Contour plots can be colour mapped using the “colormap”. For more information on colour mapping the contour plots, please check here
Look for the alternate solutions in this link where two contours can be overlaid in a subplot.
Reference information on Filled 2D contour Plot the Contor plot

Categories

Find more on Contour Plots 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!