Surf and NaN plotting bug

11 views (last 30 days)
James Noll
James Noll on 9 Oct 2019
Commented: Walter Roberson on 10 Oct 2019
Hello,
I have some data I am trying to plot with the surf command. The data has many NaN entries with small islands/peninsulas of good data. The NaN entries represent time/locations where no data is available. When I plot the data using surf(), I lose many of the finer feature - thin islands disappear. If I replace all of the NaN entries with a scalar (0), the islands reappear. Is there a workaround to plot all of the good data and have the NaN remain as blanks?
Matlab version is 2017a

Answers (2)

James Noll
James Noll on 10 Oct 2019
Edited: James Noll on 10 Oct 2019
Thank you for the information! I did not realize that each facet color was driven by the value of the neighboring facets when using surf(). Since I am only interested in the 2D view of the data, it turns out pcolor() has the functionality I need. It does not interpolate by default, so the data islands surrounded by 'nan' are no longer being blanked.
I don't have the toolbox required for warp(), so I was not able to check that as a possible solution. Below is an example output for some test data with nan's randomly distributed.
x = [1:1:100]'; y = [1:1:100]'; b = rand(size(x,1), size(y,1));
b(b<.3) = nan;
figure; surf(x,y,b); view(2); title('surf');
figure; pcolor(x,y,b); title('pcolor');
  1 Comment
Walter Roberson
Walter Roberson on 10 Oct 2019
pcolor() uses the x and y you give, and all zeros as the z, and uses the data you give as the cdata information in a call to surface() . Which is to say, it is surf() with x, y, 0, b . If you look at something like
pcolor(randi(10,5,7))
you will see that it draws (5-1) by (7-1) faces. It is doing interpolation.

Sign in to comment.


Walter Roberson
Walter Roberson on 10 Oct 2019
Edited: Walter Roberson on 10 Oct 2019
surf() and pcolor() determine the color of faces by bilinear interpolation of the four corners. Any single nan is at the corner of four different faces, so any given nan "poisons" four faces making them empty because the calculation comes out nan. It is easy for thin islands to disappear.
You should look more carefully at the CData and AlphaData properties
However when you make your decisions you are probably going to encounter the same basic issue that surf faces do not reflect individual locations, which is probably what you want.
Depending on your needs, warp() might be more useful to you. If not, then go for patch() as it offers more flexibility as to how face color is derived.

Tags

Community Treasure Hunt

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

Start Hunting!