How to plot a 2D pcolor with contours only at specific levels?
31 views (last 30 days)
Show older comments
Luís Henrique Bordin
on 24 Oct 2024 at 14:51
Commented: Star Strider
on 24 Oct 2024 at 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.
0 Comments
Accepted Answer
Star Strider
on 24 Oct 2024 at 15:03
Using the vector works here, and your code runs without error in R2024b —
load('data.mat')
whos('-file','data')
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
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!