Stuart’s MATLAB Videos

Watch and Learn

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the Original version of the page.

MATLAB Basics: Setting edge color for large surface plots 5

Posted by Doug Hull,

Often if you make a surface plot with SURF for a large dataset, it will appear all black because MATLAB is trying to draw all the edge lines. You can stop these lines from obscuring your data by turning the edge color off: a= peaks(1000); h = surf(a) set(h, 'edgecolor','none')

5 CommentsOldest to Newest

Petr Pošík replied on : 1 of 5
Hi Doug. Personally I tend to use the
shading flat, shading interp, shading faceted
commands. They take care of greater number of objects automatically, but internally use similar commands you presented in the video. Regards, Petr
Matt Fig replied on : 2 of 5
I often want the high shape resolution given by large data sets for a surface, but also want to see the net over the surface. This is useful for publication/presentation purposes. Since, as you show, using the net as is will make the whole surface black, I modify it by doing something like this:
 
a = peaks(1000);
opengl software
h = surf(a);
set(h, 'edgecolor','none')
hold on
x = 1:1000;
k = [1 0:20:1000];  % The values to keep.
k(2) = [];
st = setxor(x,k);
x(st) = NaN;  % Set most values so they won't plot.
[xx,yy] = meshgrid(x);
idx1 = isnan(xx)|isnan(yy);
a(idx1) = NaN;
idx2 = ~isnan(xx)&~isnan(yy);
xx = reshape(xx(idx2),51,51);  % Reshape these guys.
yy = reshape(yy(idx2),51,51);
aa = reshape(a(idx2),51,51);
m = mesh(xx,yy,aa+.04);  % Create the net.
set(m,'edgecolor','black','facealpha',0,'linewidth',1)
set(1,'units','normalized','position',[.05 .05 .85 .85])
 
Note that I added a small value to the aa matrix. This is because if left alone the net will have patches that are blocked by the surface, mainly at the peaks. Of course it makes the underside of the surface look worse in this regard, but as I said, I use this technique to get a static image for publication/presentation purposes.
Thomas Buhler replied on : 3 of 5
Sorry to reopen a dead thread, but I love Matt Fig's solution. I'm afraid my programming is weak, and I'm not going into a programming field. I was working on a math project and wanted to expand Matt's solution to my matrix of data when I plot it as a surface. I have variable amounts of columns and rows, and would like to make a function to just perform Matt's work on any surface created from a matrix. What I got so far was function plot_seperated_net ( surface ) [ rows , columns ] = size( surface ); lines_drawn = 10; a = surface; opengl software h = surf(a); set(h, 'edgecolor','none') hold on x = 1:columns; k = [1 0:columns/lines_drawn:columns]; % The values to keep. k(2) = []; st = setxor(x,k); x(st) = NaN; % Set most values so they won't plot. [xx,yy] = meshgrid(x); idx1 = isnan(xx)|isnan(yy); a(idx1) = NaN; idx2 = ~isnan(xx)&~isnan(yy); xx = reshape(xx(idx2),lines_drawn+1,lines_drawn+1); % Reshape these guys. yy = reshape(yy(idx2),lines_drawn+1,lines_drawn+1); aa = reshape(a(idx2),lines_drawn+1,lines_drawn+1); m = mesh(xx,yy,aa+.04); % Create the net. set(m,'edgecolor','black','facealpha',0,'linewidth',1) set(1,'units','normalized','position',[.05 .05 .85 .85]) end I'm not sure how to modify how far the lines extend into the 'y' and 'z' dimensions. So I'm only getting a small area of intersection across my whole surface. Any help would be appreciated :)
Stuart McGarrity replied on : 4 of 5
I can't completely follow Matt's code. The problem is the meshgrid plot as it uses fewer points sometimes is hidden by the surface. It works better if the number of lines are higher. You would need to draw a grid at more points on the surface to follow it better.
Stuart McGarrity replied on : 5 of 5
My colleague Mike from the MATLAB Graphics blog suggested this solution. He adds two “edge surfaces” which are each low-res on one axis and high-res on the other. It results in a simpler solution. function skip_surf(z,numLines) h = surf(z,'EdgeColor','none'); [n,m] = size(z); x1 = 1:m; y1 = 1:n; x2 = round(linspace(x1(1),x1(end),numLines)); y2 = round(linspace(y1(1),y1(end),numLines)); surface(h.XData(x1),h.YData(y2),h.ZData(y2,x1),'FaceColor','none','MeshStyle','row') surface(h.XData(x2),h.YData(y1),h.ZData(y1,x2),'FaceColor','none','MeshStyle','column') >>skip_surf(peaks(1000),15)