当前位置:网站首页>利用plot_surface命令绘制复杂曲面入门详解
利用plot_surface命令绘制复杂曲面入门详解
2022-08-02 14:10:00 【杨老头软工】
一、引言
在三维几何图形中,经常需要绘制复杂的曲面,例如封闭的空间曲面。本文以绘制由平面z=1,旋转抛物面z=x^2 + y^2和抛物柱面y=2x围成的封闭曲面为例,详细讲解利用Python命令plot_surface绘制复杂曲面的过程。
二、绘制封闭的曲面
1、首先绘制旋转抛物面z=x^2 + y^2
考虑旋转抛物面函数的定义域是圆域,因此使用极坐标来表示该图形的横坐标和纵坐标,让得到的图形在视觉上更好看一些。
具体的步骤为:
1)首先建立图形窗口,并规定使用三维坐标系
2)确定极径和极角的网格坐标
3)根据直角坐标和极坐标的关系得到旋转抛物面上的横坐标、坐标轴和竖坐标
4)利用plot_surface函数绘制旋转抛物面
5)增加每个坐标轴的标签,确定三维图形的视角
参考代码如下:
import matplotlib.pyplot as plt
import numpy as np
import math
fig = plt.figure() #建立图形窗口
ax = fig.gca( projection = '3d' ) #使用三维坐标
theta = np.arange( 0, 2 * math.pi, 0.05 )
rou = np.arange( 0, 1, 0.005 )
T, R = np.meshgrid( theta, rou ) #生成极角和极径的网格坐标
Xp = R * np.cos(T) #根据极坐标计算横坐标
Yp = R * np.sin(T) #根据极坐标计算纵坐标
Zp = Xp**2 + Yp**2 #把横坐标和纵坐标代入旋转抛物面函数得到竖坐标
ax.plot_surface( Xp, Yp, Zp, cstride = 1, rstride = 2, edgecolor = 'b' ) #绘制旋转抛物面
ax.set_xlabel( 'x' )
ax.set_ylabel( 'y' )
ax.set_zlabel( 'z' )
ax.view_init( 20, 45 )
上述代码运行结果为:
2、绘制抛物柱面y=x^2
有了上面绘制旋转抛物面的经验,此处直接上代码。
fig = plt.figure() #建立图形窗口
ax = fig.gca( projection = '3d' ) #三维坐标系
Xc, Zc = np.meshgrid( np.arange(-1, 1, 0.005), np.arange(0, 1, 0.005 ) )#自变量网格坐标
Yc = Xc**2
ax.plot_surface( Xc, Yc, Zc, cstride = 1, rstride = 2, edgecolor = 'y' )#绘图
ax.set_xlabel( 'x' )
ax.set_ylabel( 'y' )
ax.set_zlabel( 'z' )
ax.view_init( 20, 45 )
上述代码运行结果为:
3、绘制与旋转抛物面具有相同定义域的平面z=1
有了上面绘制旋转抛物面的经验,此处直接上代码。
fig = plt.figure() #建立图形窗口
ax = fig.gca( projection = '3d' ) #三维坐标系
[ m, n ] = np.shape( Xp )
Zf = np.ones( Xp.shape )
ax.plot_surface( Xp, Yp, Zf, cstride = 1, rstride = 2, edgecolor = 'r' )
ax.set_xlabel( 'x' )
ax.set_ylabel( 'y' )
ax.set_zlabel( 'z' )
ax.view_init( 20, 45 )
上述代码运行结果为:
4、把上述代码融合到一块运行就可以得到下面的图形:
注意:需要把绘制抛物柱面和平面代码中如下两条语句删除
fig = plt.figure() #建立图形窗口
ax = fig.gca( projection = '3d' ) #三维坐标系
5、删除每个图形上多余的图元
从上图可以看出,结果不尽人意,如果能够去掉各曲面多余的部分,则可以得到比较好看的封闭曲面。因此讲采用下面的代码来分别删除选择抛物面、抛物柱面和平面的多余数据,只需要令多余图元对应的竖坐标分量值为None即可。
1)删除抛物柱面多余的数据(本方法效率比较低)
[ mc, nc ] = np.shape( Xc )
for i in range( mc ):
for j in range( nc ):
if Zc[ i, j ] < Xc[ i, j ] * Xc[ i, j ] + Yc[ i, j ] * Yc[ i, j ]:
Zc[ i, j ] = None
```
2)删除选择抛物面和平面多余的数据(本方法效率比较低)
```python
[ m, n ] = np.shape( Xp )
Zf = np.ones( Xp.shape )
for i in range( m ):
for j in range( n ):
if Yp[ i, j ] < Xp[ i, j ] * Xp[ i, j ]:
Zf[ i, j ] = None
Zp[ i, j ] = None
这样就可以得到如下视觉效果较好的图形:
6、完整的绘制封闭曲面的代码
import matplotlib.pyplot as plt
import numpy as np
import math
#建立图形窗口
fig = plt.figure()
ax = fig.gca( projection = '3d' )#Axes3D( fig )
#旋转抛物面 Paraboloid 的图形数据
theta = np.arange( 0, math.pi, 0.01 )
rou = np.arange( 0, 1, 0.01 )
T, R = np.meshgrid( theta, rou )
Xp = R * np.cos(T)
Yp = R * np.sin(T)
Zp = Xp**2 + Yp**2
#抛物柱面 Parabolic Cylinder 的图形数据
Xc, Zc = np.meshgrid( np.arange(-1, 1, 0.01), np.arange(0, 1, 0.01 ) )
Yc = Xc**2
[ mc, nc ] = np.shape( Xc )
#删除抛物柱面多余的数据
for i in range( mc ):
for j in range( nc ):
if Zc[ i, j ] < Xc[ i, j ] * Xc[ i, j ] + Yc[ i, j ] * Yc[ i, j ]:
Zc[ i, j ] = None
#平面 flat surface 的图形数据
Zf = np.ones( Xp.shape )
[ m, n ] = np.shape( Xp )
#删除平面和旋转抛物面多余的数据
for i in range( m ):
for j in range( n ):
if Yp[ i, j ] < Xp[ i, j ] * Xp[ i, j ]:
Zf[ i, j ] = None
Zp[ i, j ] = None
#绘图
ax.plot_surface( Xc, Yc, Zc, cstride = 1, rstride = 1, edgecolor = 'y' )
ax.plot_surface( Xp, Yp, Zp, cstride = 1, rstride = 1, edgecolor = 'b' )
ax.plot_surface( Xp, Yp, Zf, cstride = 1, rstride = 1, edgecolor = 'r' )
ax.set_xlabel( 'x' )
ax.set_ylabel( 'y' )
ax.set_zlabel( 'z' )
ax.view_init( 20, 30 )
边栏推荐
- FP7126降压恒流65536级高辉无频闪调光共阳极舞台灯RGB驱动方案
- Win7怎么干净启动?如何只加载基本服务启动Win7系统
- 镜像法求解接地导体空腔电势分布问题
- ASR6601牛羊定位器芯片GPS国内首颗支持LoRa的LPWAN SoC
- What is Win10 God Mode for?How to enable God Mode in Windows 10?
- CS4398音频解码替代芯片DP4398完全兼容DAC解码
- Win11怎么在右键菜单添加一键关机选项
- 【使用Pytorch实现ResNet网络模型:ResNet50、ResNet101和ResNet152】
- Win10 Settings screen out from lack of sleep?Win10 set the method that never sleep
- ECP2459耐压60V降压BUCK电路用于WIFI模块供电方案原理图
猜你喜欢

FP5139电池与适配器供电DC-DC隔离升降压电路反激电路电荷泵电路原理图

Win10 cannot directly use photo viewer to open the picture

Win7 encounters an error and cannot boot into the desktop normally, how to solve it?

Mysql之MVCC

【深度学习中的损失函数整理与总结】

cmake配置libtorch报错Failed to compute shorthash for libnvrtc.so

基于51单片机和物联网的智能家居系统(ESP8266物联网模块)

基于无监督医学图像配准论文(1)

golang之GMP调度模型

为android系统添加产品的过程
随机推荐
Bash shell位置参数
【使用Pytorch实现ResNet网络模型:ResNet50、ResNet101和ResNet152】
PyTorch③---torchvision中数据集的使用
PyTorch②---transforms结构及用法、常见的Transforms
PyTorch④---DataLoader的使用
Win7怎么干净启动?如何只加载基本服务启动Win7系统
Mysql的锁
PyTorch⑨---卷积神经网络_线性层
Win7遇到错误无法正常开机进桌面怎么解决?
pygame绘制弧线
PyTorch①---加载数据、tensorboard的使用
FP7195转模拟恒流调光芯片在机器视觉光源的应用优势
PyTorch(15)---模型保存和加载
Win10 Settings screen out from lack of sleep?Win10 set the method that never sleep
LORA芯片ASR6505无线远距离传输8位MCU
CMAKE
Binder机制(下篇)
General syntax and usage instructions of SQL (picture and text)
关于c语言的调试技巧
Binder ServiceManager解析