How can I reduce the number of mesh lines shown in a surface plot in MATLAB?

66 views (last 30 days)
I would like to reduce the number of mesh lines shown on the surface plot. Although, I wish to maintain the curvature of my surface plot, that is, i do not want to reduce the number of data points.
I am unable to see any setting or method to view a different number of lines or reduce the density of the mesh.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 9 Feb 2016
There is no direct property that can reduce the number of 'edge' lines shown in the surface plot and at the same time maintain the number of data points.
However, you can make the edges in the plot invisible and manually re-plot only the desired lines. This can be done by taking only certain X and Y data points and plotting the corresponding Y-Z data and X-Z data to form new lines.
This method is also useful for correcting the issue of a surface appearing to be black due to a high density of mesh lines. 
The example code below shows how you can set the surface plot lines to user-defined grid.
 
%%Changing(reducing) the number of lines (edges / mesh) shown by a surface plot.
[X,Y,Z] = peaks(50);
figure('Position',[280 400 1200 450])
% Original surface with too many edges
subplot(1,2,1)
surf(X,Y,Z,'FaceColor','interp');
xlabel('X')
ylabel('Y')
zlabel('Z')
% Compare to:
subplot(1,2,2)
s = surf(X,Y,Z,'FaceColor','interp','EdgeColor','none');
xlabel('X')
ylabel('Y')
zlabel('Z')
%%Extract X,Y and Z data from surface plot
x=s.XData;
y=s.YData;
z=s.ZData;
% For R2014a and earlier:
% x=get(s,'XData');
% y=get(s,'YData');
% z=get(s,'ZData');
%%Create vectors out of surface's XData and YData
x=x(1,:);
y=y(:,1);
%%Divide the lengths by the number of lines needed
xnumlines = 10; % 10 lines
ynumlines = 10; % 10 partitions
xspacing = round(length(x)/xnumlines);
yspacing = round(length(y)/ynumlines);
%%Plot the mesh lines 
% Plotting lines in the X-Z plane
hold on
for i = 1:yspacing:length(y)
    Y1 = y(i)*ones(size(x)); % a constant vector
    Z1 = z(i,:);
    plot3(x,Y1,Z1,'-k');
end
% Plotting lines in the Y-Z plane
for i = 1:xspacing:length(x)
    X2 = x(i)*ones(size(y)); % a constant vector
    Z2 = z(:,i);
    plot3(X2,y,Z2,'-k');
end
hold off

More Answers (0)

Products


Release

R2015b

Community Treasure Hunt

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

Start Hunting!