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

11 visualizzazioni (ultimi 30 giorni)
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);

Risposta accettata

Dave B
Dave B il 14 Ott 2021
Modificato: Dave B il 14 Ott 2021
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 Commenti
Walter Roberson
Walter Roberson il 14 Ott 2021
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 il 14 Ott 2021
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)

Accedi per commentare.

Più risposte (1)

Image Analyst
Image Analyst il 14 Ott 2021
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 Commenti
Image Analyst
Image Analyst il 14 Ott 2021
@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 il 16 Ott 2021
@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.

Accedi per commentare.

Categorie

Scopri di più su Colormaps in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by