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

9 ビュー (過去 30 日間)
Luís Henrique Bordin
Luís Henrique Bordin 2024 年 10 月 24 日
コメント済み: Star Strider 2024 年 10 月 24 日
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.

採用された回答

Star Strider
Star Strider 2024 年 10 月 24 日
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 件のコメント
Luís Henrique Bordin
Luís Henrique Bordin 2024 年 10 月 24 日
Perfect! Thank you very much! Have a good one!
Star Strider
Star Strider 2024 年 10 月 24 日
As always, my pleasure!
You, too!

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

その他の回答 (0 件)

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by