surf command doesn't plot the value next to NaNs values

17 次查看(过去 30 天)
Hi! I don't understand why the surf command doesn't plot the value next to NaNs values in the matrix M_array. For example the second pixel of the first row is 124, but it doesn't appear.
a=[40:0.05:41];
b=[-40:-0.05:-41];
[c,d] = meshgrid(a,b);

采纳的回答

Dave B
Dave B 2021-10-14
编辑:Dave B 2021-10-14
If you think of surf as defining a bunch of little rectangles, when one vertex is missing you can't draw the rectangle.
Your description makes surf sound like a 2-D plot (the second pixel is 124), but really it's a 3-D plot, the second face in the top row is a quadrilateral that has z values of 124, 123, NaN and 102
If you only intend to look at this in 2d, you could set the z values to all be 1 (or 0, or whatever):
load M_array
a=[40:0.05:41];
b=[-40:-0.05:-41];
[c,d] = meshgrid(a,b);
surf(c,d,ones(size(M_array)),M_array)
view(2)
colormap gray
colorbar
Or just use imagesc or pcolor which are intended for the 2d case!
pcolor(c,d,M_array)
colormap gray
colorbar
  2 个评论
Walter Roberson
Walter Roberson 2021-10-14
To add to this:
surf() defines a surface plot in which the provided datapoints define vertices, and by default the color of each face is determined by bicubic interpolation of the centroid of the face relative to the vertices. If any of the vertices are NaN, then the interpolation generates NaN.
imagesc() on the other hand defines equally-spaced rectangle faces "around" each provided location, with the value of the faces being the values passed in, so a NaN only affects that one face.
Dave B
Dave B 2021-10-14
I'm sure I'm going to regret disagreeing with @Walter Roberson, but I was under the impression that the default coloring of surf uses the 'first' color for each face, where first is defined in positive x and y directions:
surf([1 2 3 4;5 6 7 8;9 10 11 12])
view(2)
colorbar
colormap(lines(7))
caxis([.5 7.5])
(Also note in my snippet in the original answer, using the NaN containing data for C and a matrix of ones for Z was sufficient to prevent the spread of a NaN vertex to its adjacent faces)

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2021-10-14
How about if you just interpolate over the nans with the regionfill() function?
load M_array
% Fill nan holes in M_array.
nanMap = isnan(M_array);
M_arrayRepaired = regionfill(M_array, nanMap)
% Rest of code:
a=[40:0.05:41];
b=[-40:-0.05:-41];
[c,d] = meshgrid(a,b);
surf(c,d,ones(size(M_arrayRepaired)),M_arrayRepaired)
view(2)
colormap gray
colorbar
  3 个评论
Image Analyst
Image Analyst 2021-10-14
@Valeria Leto, I'm not Dave. I was the one who suggested "repairing" your array by getting rid of the nan holes. (If you like that idea, you can "Vote" for my Answer.)
But beware if you use surf() the "tiles" are not the values of your array. Look at Dave's demo. A 3-by-4 array shows up with 2-by-3 colored tiles, not 3-by-4. Just wanted to make sure you knew about this quirk regarding the surf() and pcolor() functions.
Valeria Leto
Valeria Leto 2021-10-16
@Image Analyst thank you for pointing that out. Anyway if I use
surf(c,d,ones(size(M_array)),M_array)
I specify in c and d the position of M_array values, so it's ok.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Colormaps 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by