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

31 visualizaciones (últimos 30 días)
Luís Henrique Bordin
Luís Henrique Bordin el 24 de Oct. de 2024 a las 14:51
Comentada: Star Strider el 24 de Oct. de 2024 a las 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.

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by