Shading error when using surf to plot symmetric data set

2 views (last 30 days)
I'm trying to plot a temperature distribution using the surf command, but I've run into an error where my graph ends up shading the domain differently in the upper right quadrant despite the fact that the data set is symmetric. Here's a simple example to illustrate my problem:
x=[-1 0 1]
y=[-1 0 1]
z=
0 0 0
0 1 0
0 0 0
surf(x,y,z)
This results in the following plot:
No matter how many additional data points I add, I still run into this problem. Why is this happening? Is there something about the surf command that I don't understand? Thanks in advance for the help.

Accepted Answer

Nalini Vishnoi
Nalini Vishnoi on 5 May 2015
Edited: Nalini Vishnoi on 5 May 2015
Hi Lucas,
You are correct, this behavior is reproducible and can be explained as follows: The 'Facecolor' property of surf (patch object) can be set to 'none', 'flat' and 'interp': link to the documentation. The default value of the 'FaceColor' is 'flat' which uses uniform face colors. The color data at the first vertex determines the color for the entire face (and hence one quadrant was red since its first vertex had value '1'). You cannot use this value when the 'FaceAlpha' property is set to 'interp. Find below the same surf plot with 'FaceColor' property being set to 'interp':
>> s = surf(x,y,z);
>> set(s,'FaceColor', 'interp');
Now if we rotate the plot appropriately we can see that each quadrant is made up of two triangles (since all the 4 points that make up the patch are not co-planar, so the patch needs to be drawn as two triangles). You can also notice how the triangles are arranged/joined differently in the lower left and lower right quadrants (the demarcation is shown by red and green lines respectively):
>> l1 = line([0 1],[-1 0],[0 0],'Color','green');
>> l2 = line([-1 -0],[-1 0],[0 1],'Color','red');
The order and the way in which the triangles are created is decided by the 'surf' command. We can explicitly define the order of the faces/triangles, for example in the following way (though it would be a little tedious):
>> [X,Y] = meshgrid(-1:1,-1:1);
>> Z = [0 0 0; 0 1 0; 0 0 0];
>> V = [X(:) Y(:) Z(:)]; % vertices in 3D
>> F = [2 5 3; 3 5 6; 6 5 9; 9 5 8; 8 5 7; 7 5 4; 4 5 1; 1 5 2]; % order of the vertices making up the patch
>> p = patch('Faces',F,'Vertices',V);
>> set(p,'FaceVertexCData',Z(:));
>> set(p,'FaceColor','interp');
I believe this generates the figure you were expecting in your problem.
  2 Comments
Lucas
Lucas on 5 May 2015
Nalini,
Thank you very much for your help, your explanation was great. Now I actually understand what surf is doing.
Nalini Vishnoi
Nalini Vishnoi on 5 May 2015
You are very welcome, Lucas! I am glad that the explanation was of help!

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!