Problem with surf plot shading - equal z values displayed as different colours

7 views (last 30 days)
Hi,
I am generating a surface plot (MATLAB version 9.13) of some X,Y,Z data (example data stored in the attached mat file) and have noticed that the colours of the surface plot indicate different values of Z at points where Z has the same value. Please see the attached picture where two example (x,y) points, both corresponding to a z value of 2.356, are shaded in different yellow colours.
This plot was generated by:
figure
f1 = surf(X,Y,Z);
colorbar
view([0,90])
datatip(f1,X(2),Y(16));
datatip(f1,X(42),Y(16));
I would expect the shading to appear as in this scaled colour image:
figure
f2 = imagesc(Z);
colorbar
datatip(f2,2,16);
datatip(f2,42,16);
I would appreciate your help in understanding why I am seeing this discrepancy in the shading of the surface plot.
Many thanks,
Fiona

Accepted Answer

John D'Errico
John D'Errico on 16 Feb 2023
Edited: John D'Errico on 16 Feb 2023
This is likely simpler than you think. Or maybe not.
The colors for a surface are first chosen by a direct lookup into the current colormap. One color per patch. For example:
Z = [0 1 2;1 1 3];
surf(Z)
You should note there are only TWO patches shown there, each at a constant color, even though the array was a 2x3 array of values. So six colors are not shown. Only TWO different colors. Two patches, so two colors.
The level z of that surface is not constant over each patch. It varies considerably over the patches. However, if instead, I redraw the surface, but this time use an interpolated shading, now you see the difference across the two patches.
surf(Z)
shading interp
  1 Comment
Fiona N
Fiona N on 16 Feb 2023
Thank you so much, this makes perfect sense of course and I appreciate your fast response on this.
Best wishes,
Fiona

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!