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

31 Ansichten (letzte 30 Tage)
Luís Henrique Bordin
Luís Henrique Bordin am 24 Okt. 2024 um 14:51
Kommentiert: Star Strider am 24 Okt. 2024 um 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.

Akzeptierte Antwort

Star Strider
Star Strider am 24 Okt. 2024 um 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 Kommentare
Luís Henrique Bordin
Luís Henrique Bordin am 24 Okt. 2024 um 17:32
Perfect! Thank you very much! Have a good one!
Star Strider
Star Strider am 24 Okt. 2024 um 17:38
As always, my pleasure!
You, too!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by