# why surf function does not show complete data

5 views (last 30 days)
Ham Man on 8 Jul 2022
Commented: Ham Man on 8 Jul 2022
I have a plot from pcolor function which is a grid of 8X5. but when plot these data with surf function one grid of row and one of column are eliminated. Thanks for any advice! Plots are atteched.
John D'Errico on 8 Jul 2022
See my answer. The complete grid was surfed. You just need to focus on the correct thing.

John D'Errico on 8 Jul 2022
Edited: John D'Errico on 8 Jul 2022
Um, EXACTLY what is not shown? :)
LOOK CAREFULLY at the surface you show. You surfed a 8x5 grid, right? Now, count the lines shown on that surface. Do you see 8 lines along one edge, and 5 lines along the other?
Instead, you seem to be counting the patches. Yes, there is a 7x4 grid of patches. But it is the lines that matter. surf does not plot patches at a constant level. Instead, it plots a quasi-rectangular patch connecting 4 points. Then it shows that patch in some color. But it is the EDGES that matter, NOT the patch colors. The patch color is just there to make it look pretty and help you to see the surface.
For example. consider this case, with my own 8x5 grid.
[x,y] = meshgrid(1:5,1:8);
z = x + y
z = 8×5
2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9 6 7 8 9 10 7 8 9 10 11 8 9 10 11 12 9 10 11 12 13
surf(x,y,z)
xlabel x
ylabel y
Look carefully now, and count. z was a 8x5 array. So there are 5 lines perpendicular to the x axis, and 8 lines running perpendicular to the y axis.
Again, we see only 7x4 patches. But that is not what you need to be looking at. It is the black lines that correspond to the grid you plotted. Perhaps better is to completely turn off the facets, to then see what is being plotted. Do you see here, that the grid plotted really is a 8x5 grid? The facets you saw in the surf plot were just an illusion.
mesh(x,y,z,'facecolor','none')
Perhaps another way to look at it is to use a shaded surface, with interpolated shading.
H = surf(x,y,z);
H.FaceColor = 'interp';
Again, we see that the real surface is truly derived from an 8x5 grid. COUNT THE EDGES! The patches here now have interpolated colors, which does not suggest a 7x4 array, as does the basic result that comes from surf.
My guess is it is not that hard to be confused. But that is what happened. You mistook the patches as showing what was significant, but in reality, it is the edges that really matter. (Note that pcolor and surf are in reality the same thing, except that pcolor is just a surf viewed from directly above.)
pcolor(x,y,z)
colorbar
I even added a colorbar there. Lets do it using surf too.
surf(x,y,z)
view(0,90)
So pcolor is just a call to surf, then shifting the view angle to directly above.
If you look carefully at the code for pcolor, you will see this line of code near the end:
set(cax,'View',[0 90]);
Surely you CANNOT say there is any difference in what pcolor shows, compared to surf? It is the same 8x5 grid. The colored patches are identically the same number whether pcolor or surf plotted them, always a 7x4 grid of PATCHES. But it is the lines that are what correspond to what is plotted. Don't focus on the patches.
Ham Man on 8 Jul 2022
Thank you so much John for great explanation.
I figured out my x and y from meshgrid are not the same size as C matrix and I had to add one row of NaN to C to make it the same size as x and y. Thats the only reason I think. (pcolor(x,y,C))
x =
127.2792 247.4874 367.6955 487.9037 608.1118 728.3200
127.2792 247.4874 367.6955 487.9037 608.1118 728.3200
127.2792 247.4874 367.6955 487.9037 608.1118 728.3200
127.2792 247.4874 367.6955 487.9037 608.1118 728.3200
127.2792 247.4874 367.6955 487.9037 608.1118 728.3200
127.2792 247.4874 367.6955 487.9037 608.1118 728.3200
127.2792 247.4874 367.6955 487.9037 608.1118 728.3200
127.2792 247.4874 367.6955 487.9037 608.1118 728.3200
y =
227.2444 227.2444 227.2444 227.2444 227.2444 227.2444
260.1178 260.1178 260.1178 260.1178 260.1178 260.1178
292.9912 292.9912 292.9912 292.9912 292.9912 292.9912
325.8645 325.8645 325.8645 325.8645 325.8645 325.8645
358.7379 358.7379 358.7379 358.7379 358.7379 358.7379
391.6113 391.6113 391.6113 391.6113 391.6113 391.6113
424.4847 424.4847 424.4847 424.4847 424.4847 424.4847
457.3581 457.3581 457.3581 457.3581 457.3581 457.3581
C=
5 12 18 12 21 18 9 5
87 193 187 183 207 195 192 64
177 219 225 226 222 256 246 236
79 221 242 234 273 288 226 104
17 37 56 67 73 69 40 14
NaN NaN NaN NaN NaN NaN NaN NaN