Home > Net >  coloring 3D plots on MATLAB
coloring 3D plots on MATLAB

Time:04-28

I made two 3D plots on the same axis. now I desire to give them different colors for easy identification. How do I do this coloring? The MATLAB code is shown below.

tic
Nx = 50;
Ny = 50;
x = linspace(0,1,Nx);
y = linspace(0,0.5,Ny);
[X,Y] = meshgrid(x,y);
[M,N] = size(X);
 for m=1:M
    for n=1:N
        %get x,y coordinate
        x_mn = X(m,n);
        y_mn = Y(m,n);
        %%% X=D2   and Y=D1
        %Check if x_mn and y_mn satisfy requirement
        if(x_mn >= y_mn)
            %evaluate function 1
           Z(m,n) =  (x_mn^2 - 2*x_mn*y_mn   y_mn^2);
           Z_1(m,n) =  (x_mn^2);
        elseif(x_mn < y_mn)
            %evaluate function 2
            Z(m,n) =  0;
            Z_1(m,n) =  (x_mn^2);
            %% Z(m,n) = 2*(2*x_mn*y_mn   y_mn - y_mn^2 - 2*x_mn);
        else 
            Z(m,n) = 0;
       
        end
    end
end
%Plot the surface
figure
surf(X,Y,Z)   %first plot
surfc(X,Y,Z)
hold on
surf(X,Y,Z_1)   %second plot
xlabel('Dm');
ylabel('D');
zlabel('pR');
grid on
shading interp
toc
disp('DONE!')

How can I create two differently colored surfaces?

CodePudding user response:

figure
surf(X,Y,Z)   %first plot
surfc(X,Y,Z)
hold on
surf(X,Y,Z_1)

Your surfc() call actually overwrites your surf() call, is this intended?

As to your colour: the enter image description here

  • Related