Search code examples
matlab

Threshold on contourf


I want two contour plot to show a threshold and I'm using

contourf(X,Y,Z,[-1,-1],'k');
contourf(X,Y,Z,[2,2],'w');

while X and Y a simple meshgrid.

I see that the matrix Z has values above -1 and above 1, so I'm expecting the two lines at -1 and 1 but I see only the first contour lines. What am I doing wrong? Do I get it wrong this property?

Edit: here an example

x = 0:5:180;
y = 0:5:355;
[X,Y] = meshgrid(x,y);
Z = 5*cos(X);

figure(1);## Heading ##
surf(X',Y',Z','EdgeColor','none');
view(2); colorbar;colormap(jet); hold on
contour(X',Y',Z',[-1,-1],'k','LineWidth',1);
contour(X',Y',Z',[2,2],'k','LineWidth',2);
legend('','1','2','Location','northeastoutside');

I saw that if I put the two contour in a new figure, the two are plotted. But when I use surf, I just see the [-1,-1] option. Many thanks

edit2: using pcolor instead of surf (removing ,'EdgeColor','none') gives me the correct plot...Why? What changes?


Solution

  • What happens is that contourf is not a 3D ploting function, its a 2D plotting function. So when you ask to plot contour lines, it will always plot them at z=0. So your surf plot is "on top of" the contourf for z>0, as you are setting view(2).

    Look at the example you gave, with a different view:

    enter image description here

    You can see both lines here.

    pcolor plots a 2D image, not a 3D surface, so it works with pcolor or imagesc.