How to plot a 2D pcolor with contours only at specific levels?

Hey guys, please, could someone help me with this. I am trying to plot a 2D pcolor figure as longitude X depth, and water density as colors. I also want to add contours, just at specific color levels, but I just was able to do it with same number of colors and contour levels, this way:
pcolor(lons,zs2,dens);
hold on
contourf(lons,zs2,dens,20,'k');
ax.FontSize = 6;
shading interp
colormap(parula(16))
caxis([1020 1026])
ylim([-1000 0])
ylabel('Depth (m)','fontsize',6,'fontweight','b')
xlabel('Longitude','fontsize',4,'fontweight','b')
in the contourf, 20 is the number of levels. But actually I want specific levels, e.g., [-0.3 0 0.3]. The function help says it can be a vector, but when I try, I get an error saying it must be a matrix the same size of longitude X depth. Then, I imagine I need to build a matrix containing only the values correcponding to [-0.3 0 0.3], and the remaning as NaNs. Am I correct? If yes, please, how can I do that? Or if I am wrong, there is another way to do what I want? data attached!
Thank you very much in advance.

 Accepted Answer

Using the vector works here, and your code runs without error in R2024b
load('data.mat')
whos('-file','data')
Name Size Bytes Class Attributes dens 218x37 64528 double lons 218x37 64528 double zs2 218x37 64528 double
pcolor(lons,zs2,dens);
hold on
contourf(lons,zs2,dens,[-0.3 0 0.3],'k');
ax.FontSize = 6;
shading interp
colormap(parula(16))
caxis([1020 1026])
ylim([-1000 0])
ylabel('Depth (m)','fontsize',6,'fontweight','b')
xlabel('Longitude','fontsize',4,'fontweight','b')
You must be using a different ‘Z’ matrix in the code that throws the error.
.

4 Comments

I was replacing dens by [-0.3 0 0.3], not adding it. Now it worked here, however, the image is wrong, it should show the different colors and levels as below.
To do that, use contour rather than contourf
load('data.mat')
whos('-file','data')
Name Size Bytes Class Attributes dens 218x37 64528 double lons 218x37 64528 double zs2 218x37 64528 double
[dmin,dmax] = bounds(dens(:))
dmin = 1.0121e+03
dmax = 1.0258e+03
densrange = dmax-dmin
densrange = 13.6701
densmean = mean(dens(:))
densmean = 1.0228e+03
densmedian = median(dens(:))
densmedian = 1.0230e+03
levelv = [1023 1023.4 1023.75 1024.15 1024.5 1024.9]; % This Is As Close As I Can Get TTo The Contours In The Image
figure
pcolor(lons,zs2,dens);
hold on
[c,h] = contour(lons,zs2,dens,levelv,'-k', 'ShowText',1);
h.ZLocation = 'zmax';
ax.FontSize = 6;
shading interp
colormap(parula(16))
caxis([1020 1026])
ylim([-1000 0])
ylabel('Depth (m)','fontsize',6,'fontweight','b')
xlabel('Longitude','fontsize',4,'fontweight','b')
levelv = linspace(1021, 1025, 10)
levelv = 1×10
1.0e+03 * 1.0210 1.0214 1.0219 1.0223 1.0228 1.0232 1.0237 1.0241 1.0246 1.0250
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
figure
surf(lons,zs2,dens, 'EdgeColor','interp', 'FaceAlpha',0.5)
grid on
hold on
[c,h] = contour3(lons,zs2,dens,levelv,'-k', 'LineWidth',2);
hold off
colormap(parula(16))
caxis([1020 1026])
ylim([-1000 0])
zlim([1021 dmax])
I can’t get the contour lines to appear here. The chosen levels are completely outside the range of the data, and tweaking them to have something in common with it is apparently not possible. You probably need to tweak them to show the correct values.
I added a manual tweak in the first plot. Yopu may want to tweak them further.
I provided a second plot that shows the surface, and a way to determine the contour levels (the ‘levelv’ vector). I defer to you to experiment with that to get the result you want. The [-0.3 0 0.3] vector is clearly not working here.
EDIT — (24 Oct 2024 at 16:47)
I added a manual tweak in the first plot. You may want to tweak them further.
.
Perfect! Thank you very much! Have a good one!
As always, my pleasure!
You, too!

Sign in to comment.

More Answers (0)

Products

Release

R2019b

Community Treasure Hunt

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

Start Hunting!