简体   繁体   English

在 MATLAB 上着色 3D 图

[英]coloring 3D plots on MATLAB

I made two 3D plots on the same axis.我在同一轴上绘制了两个 3D 图。 now I desire to give them different colors for easy identification.现在我想给他们不同的 colors 以便于识别。 How do I do this coloring?我该怎么做这个着色? The MATLAB code is shown below. MATLAB 代码如下所示。

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?如何创建两个不同颜色的表面?

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?您的surfc()调用实际上覆盖了您的surf()调用,这是故意的吗?

As to your colour: the documentation is a marvellous thing:至于你的颜色:文档是一件了不起的事情:

surfc(X,Y,Z,C) additionally specifies the surface color. surfc(X,Y,Z,C)还指定表面颜色。

In other words: just specify the colour as you want it.换句话说:只需指定您想要的颜色即可。 C needs to be a matrix of size(Z) with the desired colours, ie set all of them equal to create an monocoloured surface: C需要是具有所需颜色的size(Z)的矩阵,即将它们全部设置为相等以创建单色表面:

x = 1:100;
y = 1:100;
z = rand(100);

figure;
surfc(x,y,z,ones(size(z)))
hold on
surfc(x,y,z+6,ones(size(z))+4)

Results in (MATLAB R2007b, but the syntax is the same nowadays)结果为(MATLAB R2007b,但如今语法相同)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM