您当前的位置: 首页 >  Python

wendy_ya

暂无认证

  • 2浏览

    0关注

    342博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

三维曲面的绘制(Python/MATLAB)

wendy_ya 发布时间:2020-09-01 19:22:26 ,浏览量:2

目录
      • 一、利用Python绘制三维曲面
        • 1.导入相应的包:
        • 2.创建二维平面网格meshgrid()
        • 3.添加颜色棒
        • 4.facecolors自定义颜色
        • 5.综合代码
      • 二、利用MATLAB绘制三维曲面
        • 1.surf方法
        • 2.mesh方法
        • 3.contour方法
        • 4.contourf方法
        • 5.meshc方法
        • 6.meshz方法
        • 7.contour3方法

一、利用Python绘制三维曲面

以求z=x^2 + y^2为例,其步骤如下:

1.导入相应的包:
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D#画3d图案所必需的
2.创建二维平面网格meshgrid()
#z=x^2+y^2

x=np.linspace(-1,1,100)
y=np.linspace(-1,1,50)

x_mesh,y_mesh=np.meshgrid(x,y,indexing='ij')#生成网络,参数'ij'生成网格形状是第一个维度对应x,第二个维度对应y
#print(x_mesh.shape,y_mesh.shape)

z_mesh=x_mesh**2+y_mesh**2



fig=plt.figure()
sub=fig.add_subplot(111,projection='3d')#3d表示三维图像
sub.plot_surface(x_mesh,y_mesh,z_mesh,color='blue')

sub.set_xlabel(r'$x$')
sub.set_ylabel(r'$y$')
sub.set_zlabel(r'$z$')

plt.show()

图像如图所示: 在这里插入图片描述

但是这样有一个问题,color只能设置单一的颜色,而不能设置变化的颜色。可以调用cmap参数来选择颜色表,并且它也可以是color不起作用。

cmp=plt.cm.Purples

效果如图: 在这里插入图片描述 可以看到,这时的图像就成了渐变色。 关于matplotlib.cm的相关参数可以参考官方介绍:https://matplotlib.org/api/cm_api.html

关于内置颜色表,可以参考: https://matplotlib.org/gallery/color/colormap_reference.html

在这里插入图片描述 代码中我使用的是渐变紫色——Purples,当然可以更换为任意颜色。

3.添加颜色棒

根据z的值设置颜色棒:

surf=sub.plot_surface(x_mesh,y_mesh,z_mesh,cmap=plt.cm.Purples)#设置渐变色
cb=fig.colorbar(surf,shrink=0.8,aspect=15,label='$z(x,y)$')#添加颜色棒,shrink表示缩放,aspect表示宽

结果如图: 在这里插入图片描述 当然,也可以人为设置曲面颜色。方法如下:

4.facecolors自定义颜色

相关代码如下:

colors_ref=np.zeros(z_mesh.shape)#定义一个数组
for i in range(len(x)):    #z_mesh.shape[0]
    for j in range(len(y)): #z_mesh.shape[1]
        colors_ref[i,j]=np.abs(y_mesh[i,j])#abs方面后面的归一化
#因为后面要调用颜色表函数,所以要归一化
colors_norm=colors_ref/np.max(colors_ref)#介于(0,1)之间

fig=plt.figure()
sub=fig.add_subplot(111,projection='3d')#3d表示三维图像
surf=sub.plot_surface(x_mesh,y_mesh,z_mesh,cmap=plt.cm.Purples,facecolors=plt.cm.Purples(colors_norm))#设置渐变色

结果如图:(相对于x等于0对称) 在这里插入图片描述 当然也可以相对于y=0对称: 在这里插入图片描述

在这里插入图片描述

5.综合代码
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D#画3d图案所必需的

#z=x^2+y^2

x=np.linspace(-1,1,100)
y=np.linspace(-1,1,50)

x_mesh,y_mesh=np.meshgrid(x,y,indexing='ij')#生成网络,参数'ij'生成网格形状是第一个维度对应x,第二个维度对应y
#print(x_mesh.shape,y_mesh.shape)

z_mesh=x_mesh**2+y_mesh**2

colors_ref=np.zeros(z_mesh.shape)#定义一个数组
for i in range(len(x)):    #z_mesh.shape[0]
    for j in range(len(y)): #z_mesh.shape[1]
        colors_ref[i,j]=np.sqrt(x_mesh[i,j]**2+y_mesh[i,j]**2)
#因为后面要调用颜色表函数,所以要归一化
colors_norm=colors_ref/np.max(colors_ref)#介于(0,1)之间

fig=plt.figure()
sub=fig.add_subplot(111,projection='3d')#3d表示三维图像
surf=sub.plot_surface(x_mesh,y_mesh,z_mesh,cmap=plt.cm.Purples,facecolors=plt.cm.Purples(colors_norm))#设置渐变色


cb=fig.colorbar(surf,shrink=0.8,aspect=15,label='$\sqrt{x^2+y^2}$')#添加颜色棒,shrink表示缩放,aspect表示宽
cb.set_ticks([0,0.5,1,np.sqrt(2)]/np.sqrt(2))
cb.ax.set_yticklabels(['0','0.5','1',r'$\sqrt{2}$'])#设置颜色棒刻度标签

sub.set_xlabel(r'$x$')
sub.set_ylabel(r'$y$')
sub.set_zlabel(r'$z$')

plt.show()

效果图: 在这里插入图片描述

二、利用MATLAB绘制三维曲面 1.surf方法

代码:

x=linspace(-1,1,100);y=linspace(-1,1,50);
[X,Y]=meshgrid(x,y);
Z=power(X,2)+power(Y,2);

i=1;
figure(i)
surf(X,Y,Z)
xlabel('x');ylabel('y');zlabel('z');

效果如图: 在这里插入图片描述 如果想要拖拽,可以点击这个键: 在这里插入图片描述

2.mesh方法

代码:

mesh(X,Y,Z)

效果: 在这里插入图片描述

mesh与surf的区别: mesh没有填充,而surf填充了。

3.contour方法

代码:

contour(X,Y,Z,'ShowText','on')
xlabel('x');ylabel('y');
xlim([-1,1]);ylim([-1,1]);

效果: 在这里插入图片描述

4.contourf方法

代码:

contourf(X,Y,Z,'ShowText','on')
xlabel('x');ylabel('y');
xlim([-1,1]);ylim([-1,1]);

效果: 在这里插入图片描述

5.meshc方法

代码:

meshc(X,Y,Z)
xlabel('x');ylabel('y');zlabel('z');

效果: 在这里插入图片描述

6.meshz方法

代码:

meshz(X,Y,Z)
xlabel('x');ylabel('y');zlabel('z');

效果: 在这里插入图片描述

7.contour3方法

代码:

contour3(X,Y,Z,'ShowText','on')
xlabel('x');ylabel('y');
xlim([-1,1]);ylim([-1,1]);

效果: 在这里插入图片描述

关注
打赏
1659256378
查看更多评论
立即登录/注册

微信扫码登录

0.0780s