当前位置: 首页 > news >正文

Python绘制相交平面

该示例演示了在3D中绘制相交平面。这是二维图像到三维的一般化。

在mplot3d中绘制相交平面比较复杂,因为mplot3d并不是真正的3D渲染器,它只是将艺术家(Artists)投影到3D中并按正确的顺序绘制它们。如果艺术家彼此重叠,这种方法就不能正确工作。在此示例中,我们通过在平面交点处分段来解决相互重叠的问题,将每个平面分成四部分。

该示例仅在平面相互平分切割时能够正确工作。这个限制是有意为之,以保持代码更易读。虽然在任意位置切割平面是可能的,但会使代码更加复杂。因此,该示例更多是展示如何绕过3D可视化限制的概念,而不是一个可以直接复制粘贴用于绘制任意相交平面的完善解决方案。

import matplotlib.pyplot as plt
import numpy as np


def plot_quadrants(ax, array, fixed_coord, cmap):
"""For a given 3d *array* plot a plane with *fixed_coord*, using four quadrants."""
nx, ny, nz = array.shape
index = {
'x': (nx // 2, slice(None), slice(None)),
'y': (slice(None), ny // 2, slice(None)),
'z': (slice(None), slice(None), nz // 2),
}[fixed_coord]
plane_data = array[index]

n0, n1 = plane_data.shape
quadrants = [
plane_data[:n0 // 2, :n1 // 2],
plane_data[:n0 // 2, n1 // 2:],
plane_data[n0 // 2:, :n1 // 2],
plane_data[n0 // 2:, n1 // 2:]
]

min_val = array.min()
max_val = array.max()

cmap = plt.get_cmap(cmap)

for i, quadrant in enumerate(quadrants):
facecolors = cmap((quadrant - min_val) / (max_val - min_val))
if fixed_coord == 'x':
Y, Z = np.mgrid[0:ny // 2, 0:nz // 2]
X = nx // 2 * np.ones_like(Y)
Y_offset = (i // 2) * ny // 2
Z_offset = (i % 2) * nz // 2
ax.plot_surface(X, Y + Y_offset, Z + Z_offset, rstride=1, cstride=1,
facecolors=facecolors, shade=False)
elif fixed_coord == 'y':
X, Z = np.mgrid[0:nx // 2, 0:nz // 2]
Y = ny // 2 * np.ones_like(X)
X_offset = (i // 2) * nx // 2
Z_offset = (i % 2) * nz // 2
ax.plot_surface(X + X_offset, Y, Z + Z_offset, rstride=1, cstride=1,
facecolors=facecolors, shade=False)
elif fixed_coord == 'z':
X, Y = np.mgrid[0:nx // 2, 0:ny // 2]
Z = nz // 2 * np.ones_like(X)
X_offset = (i // 2) * nx // 2
Y_offset = (i % 2) * ny // 2
ax.plot_surface(X + X_offset, Y + Y_offset, Z, rstride=1, cstride=1,
facecolors=facecolors, shade=False)


def figure_3D_array_slices(array, cmap=None):
"""Plot a 3d array using three intersecting centered planes."""
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.set_box_aspect(array.shape)
plot_quadrants(ax, array, 'x', cmap=cmap)
plot_quadrants(ax, array, 'y', cmap=cmap)
plot_quadrants(ax, array, 'z', cmap=cmap)
return fig, ax


nx, ny, nz = 70, 100, 50
r_square = (np.mgrid[-1:1:1j * nx, -1:1:1j * ny, -1:1:1j * nz] ** 2).sum(0)

figure_3D_array_slices(r_square, cmap='viridis_r')
plt.show()

参考文献:https://matplotlib.org/stable/gallery/mplot3d/intersecting_planes.html

http://www.jsqmd.com/news/454299/

相关文章:

  • 2026生物医药行业纯水设备优质品牌推荐榜 - 优质品牌商家
  • 养护策略优化模型:从单设备到路网级的多维决策系统
  • openclaw 调用openai codex , python 调用
  • 如何利用熊猫智汇赋能数字员工突破业务壁垒?
  • ORA-29660错误找不到EXTERNAL NAME定义的类,远程处理故障修复思路分享
  • 吐血整理,性能测试总结汇总,多个视角分析性能压测,一篇通透...
  • 快手 C++ 面试题:如何突破封装访问私有成员?
  • 基于电解槽与甲烷反应器构建低碳综合能源系统优化策略:购能成本、碳排放与弃风成本最小化实践指导文献研究
  • 代码随想录算法营完结!
  • 全网最细,web端测试常见与最有意义的bug(总结)
  • Abaqus与Matlab联合应用:直齿轮、斜齿轮模型调试及裂纹磨损故障刚度分析,稳态瞬态温度场研究
  • 2026国家金融监督管理总局国考计算机岗·经济金融基础全解析:技术人必掌握的15大核心考点与实战题库(附2025央行最新政策+真题示例+答题策略)
  • 2026年3月江苏徐州室内装修/室内设计/全屋定制/精装局改/软装搭配公司竞争格局深度分析报告 - 2026年企业推荐榜
  • 《计算机组成原理》细致学:计算机的功能部件
  • RecyclerView 缓存与复用机制:从一次滑动讲明白(2026 版)
  • AI写论文法宝!这4款AI论文写作工具,实现论文快速原创生成!
  • SQL自学:怎么创建视图
  • 【信道估计】基于matlab大规模MIMO-OFDM系统的5G通信信道估计算法研究【含Matlab源码 15125期】含文献
  • 用H Builder X做一个简单HTML网页
  • 深度解析Apache Fesod 2.0:重新定义Java生态高性能Excel处理的天花板
  • 2026四川不锈钢水箱采购必看:钢联建环保18项专利与500台套产能解析 - 深度智识库
  • Maven 中 test 的真正含义:限制测试类专用 打包自动跳过测试
  • 深度解析:飞扬集成设计系统如何实现建筑工程全流程数字化?
  • 客路商品详情页前端性能优化实战
  • 软件工程毕设最全开题帮助
  • AI专著生成秘籍:高效工具大揭秘,快速完成专业学术专著
  • P15129 [ROIR 2026] 筹码放置 - Link
  • 基于大数据+Hadoop+微信小程序的直播带货商品数据分析系统设计与开发(源码+精品论文+答辩PPT等资料)
  • 基于MATLAB元胞自动机(CA)的AZ80A镁合金动态再结晶(DRX)过程模拟
  • 百年产品研发管理演进史:从流水线到AI原生(1920-2026)