MATLAB上的3D图

发布于 2025-01-24 15:09:12 字数 992 浏览 0 评论 0原文

我在同一轴上制作了两个3D图。现在,我希望给他们不同的颜色,以方便识别。我该如何进行这种着色? 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!')

如何创建两个不同颜色的表面?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一江春梦 2025-01-31 15:09:12
figure
surf(X,Y,Z)   %first plot
surfc(X,Y,Z)
hold on
surf(X,Y,Z_1)

您的surfc()呼叫实际覆盖您的surf()调用,这是打算的吗?

至于您的颜色:文档

surfc(x,y,z,c)另外指定表面颜色。

换句话说:只需按照您的意愿指定颜色即可。 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)

结果 :当今语法是相同的)

”在此处输入图像描述”

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 documentation is a marvellous thing:

surfc(X,Y,Z,C) additionally specifies the surface color.

In other words: just specify the colour as you want it. C needs to be a matrix of size(Z) with the desired colours, i.e. set all of them equal to create an monocoloured surface:

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)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文