Search code examples
matlabplot3dmatlab-figure

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


I've found this answer, but I can't complete my work. I wanted to plot more precisely the functions I am studying, without overcoloring my function with black ink... meaning reducing the number of mesh lines. I precise that the functions are complex.

I tried to add to my already existing code the work written at the link above. This is what I've done:

r = (0:0.35:15)';                        % create a matrix of complex inputs
theta = pi*(-2:0.04:2);
z = r*exp(1i*theta);
w = z.^2;

figure('Name','Graphique complexe','units','normalized','outerposition',[0.08 0.1 0.8 0.55]);

s = surf(real(z),imag(z),imag(w),real(w));    % visualize the complex function using surf
s.EdgeColor = 'none';

x=s.XData;
y=s.YData;
z=s.ZData;

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);
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

But the problem is that the mesh is still invisible. How to fix this? Where is the problem? And maybe, instead of drawing a grid, perhaps it is possible to draw circles and radiuses like originally on the graph?


Solution

  • I found an old script of mine where I did more or less what you're looking for. I adapted it to the radial plot you have here.

    There are two tricks in this script:

    1. The surface plot contains all the data, but because there is no mesh drawn, it is hard to see the details in this surface (your data is quite smooth, this is particularly true for a more bumpy surface, so I added some noise to the data to show this off). To improve the visibility, we use interpolation for the color, and add a light source.

    2. The mesh drawn is a subsampled version of the original data. Because the original data is radial, the XData and YData properties are not a rectangular grid, and therefore one cannot just take the first row and column of these arrays. Instead, we use the full matrices, but subsample rows for drawing the circles and subsample columns for drawing the radii.

    resulting plot

    % create a matrix of complex inputs
    % (similar to OP, but with more data points)
    r = linspace(0,15,101).';
    theta = linspace(-pi,pi,101);
    z = r * exp(1i*theta);
    w = z.^2;
    
    figure, hold on
    
    % visualize the complex function using surf
    % (similar to OP, but with a little bit of noise added to Z)
    s = surf(real(z),imag(z),imag(w)+5*rand(size(w)),real(w));
    s.EdgeColor = 'none';
    s.FaceColor = 'interp';
    
    % get data back from figure
    x = s.XData;
    y = s.YData;
    z = s.ZData;
    
    % draw circles -- loop written to make sure the outer circle is drawn
    for ii=size(x,1):-10:1
       plot3(x(ii,:),y(ii,:),z(ii,:),'k-');
    end
    
    % draw radii
    for ii=1:5:size(x,2)
       plot3(x(:,ii),y(:,ii),z(:,ii),'k-');
    end
    
    % set axis properties for better 3D viewing of data
    set(gca,'box','on','projection','perspective')
    set(gca,'DataAspectRatio',[1,1,40])
    view(-10,26)
    
    % add lighting
    h = camlight('left');
    lighting gouraud
    material dull