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

31 vues (au cours des 30 derniers jours)
Luís Henrique Bordin
Luís Henrique Bordin le 24 Oct 2024 à 14:51
Commenté : Star Strider le 24 Oct 2024 à 17:38
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.

Réponse acceptée

Star Strider
Star Strider le 24 Oct 2024 à 15:03
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 commentaires
Luís Henrique Bordin
Luís Henrique Bordin le 24 Oct 2024 à 17:32
Perfect! Thank you very much! Have a good one!
Star Strider
Star Strider le 24 Oct 2024 à 17:38
As always, my pleasure!
You, too!

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by