Do Matlabs 3D plots swap axes?
Show older comments
Dear all,
I am very confused by Matlab's 3D plotting functions (surf, plot3). Consider the following MWE:
>> [X,Y] = meshgrid(1:20,1:20);
>> Z = sin(X) + cos(Y);
>> Z(1,end)=10;
>> surf(X,Y,Z)
>> xlabel('x')
>> ylabel('y')
I expected the command Z(1,end)=10; to set a peak at the first value of the x axis and the last value of the y axis. However, it generates the following plot, in which the axes seem to be flipped. Please point me to my error. I can reproduce this behavior for plot3 as well.

Accepted Answer
More Answers (1)
dpb
on 28 May 2018
Not the way meshgrid is laid out -- look at a small enough space you can see it easily (and also not symmetric so can tell who's who in the zoo):
>> [X,Y]=meshgrid(1:4,1:6)
X =
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
Y =
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
6 6 6 6
>>
The location you're looking for is Z(end,1) instead of Z(1,end)
5 Comments
Walter Roberson
on 29 May 2018
I find meshgrid() to be frustrating, and always feel as if it is giving the first two outputs in the wrong order. I always prefer to use ndgrid().
The only way I can explain meshgrid() is to mumble to myself that it is trying to compensate for the fact that in MATLAB images, X is the second dimension (columns) and Y is the first dimension (rows); meshgrid() feels like a hack to silently allow people to get away with treating X as if it were the first dimension.
"... always feel as if it is giving the first two outputs in the wrong order."
They are!
- With ndgrid the Nth output increases along the Nth dimension.
- With meshgrid the first two outputs are swapped: the first output increases along the second dimension and the second output increases along the first dimension (but the third is consistent!). Blame the mathematicians who decided that rows come first!
Jan
on 29 May 2018
The unexpected order of outputs concerns gradient also.
I didn't say was intuitive, only that's the way meshgrid has always worked... :)
I don't know, but I strongly suspect it exists as is simply owing to historical reasons that it was introduced that way initially probably without a tremendous amount of thought regarding the order peculiarity.
NDGRID came along somewhat later albeit still a long time back but did fix the inconsistency.
Probably would be worthy of a documentation improvement suggestion to make specific note of the difference in MESHGRID() similar to that in NDGRID that specifically notes the difference. Although MESHGRID describes the output, it isn't easy to decipher from just the words...
Have to admit I've never really thought about it too much; the general purpose is to simply display a surface and the specifics of how the ordering was arranged for the purpose hasn't ever really cropped up as an issue...
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!