surf command doesn't plot the value next to NaNs values
조회 수: 5 (최근 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);
댓글 수: 0
채택된 답변
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
pcolor(c,d,M_array)
colormap gray
colorbar
댓글 수: 2
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
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
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
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!