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

조회 수: 7 (최근 30일)
Valeria Leto
Valeria Leto 2021년 10월 14일
댓글: Valeria Leto 2021년 10월 16일
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