在球体内绘制给定顶点的3D表面

5 投票
2 回答
2893 浏览
提问于 2025-04-18 03:48

我有6个点,这些点都在一个球的表面上,并且是一个八面体的顶点。请问我该如何在3D坐标轴上绘制这个八面体的表面,让它看起来在球里面呢?

我有以下的代码,但它并没有达到我想要的效果:

from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import matplotlib.pyplot as plt

Points=[[ 0.17770898,  0.72315927,  0.66742804],
       [-0.65327074, -0.4196453 ,  0.63018661],
       [ 0.65382635,  0.42081934, -0.62882604],
       [-0.17907021, -0.72084723, -0.66956189],
       [-0.73452809,  0.5495376 , -0.39809158],
       [ 0.73451554, -0.55094017,  0.39617148]]

fig=plt.figure()
ax =fig.add_subplot(1, 1, 1, projection='3d', aspect='auto')

ax.add_collection3d(Poly3DCollection([Points]))

u = np.linspace(0, np.pi, 30)
v = np.linspace(0, 2 * np.pi, 30)

x = np.outer(np.sin(u), np.sin(v))
y = np.outer(np.sin(u), np.cos(v))
z = np.outer(np.cos(u), np.ones_like(v))

ax.plot_wireframe(x, y, z, alpha=0.3)

plt.show()

这是当前的绘图结果。没有错误,但八面体的表面看起来不对。

enter image description here

2 个回答

2

一个 Poly3DCollection 是一组多边形,而一个多边形又是一组点。每个点包含三个数值。所以你需要给 Poly3DCollection 传递一个包含多层列表的值。请修改下面的代码:

ax.add_collection3d(Poly3DCollection([Points]))
4

在HYRY的回答基础上补充一下;一个体积是由多个多边形面组成的,而每个面又是由一系列点构成的。(如果这些面是相邻的,那么每个点在这些点的列表中会出现多次)。下面是一个示例代码片段,其中点已经被标记出来了。

python 3.11.2matplotlib 3.7.1numpy 1.24.3 中测试过

from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure ()
ax = fig.add_subplot (1, 1, 1, projection = '3d', aspect = 'auto')

# octahedron
A = [ 0.17770898,  0.72315927,  0.66742804]
B = [-0.65327074, -0.4196453 ,  0.63018661]
C = [ 0.65382635,  0.42081934, -0.62882604]
D = [-0.17907021, -0.72084723, -0.66956189]
E = [-0.73452809,  0.5495376 , -0.39809158]
F = [ 0.73451554, -0.55094017,  0.39617148]
OCTO = [[E, A, B],
        [E, B, D],
        [E, D, C],
        [E, C, A],
        [F, A, B],
        [F, B, D],
        [F, D, C],
        [F, C, A],
]
ax.add_collection3d (Poly3DCollection (OCTO))

# sphere
u = np.linspace (0, np.pi, 30)
v = np.linspace (0, 2 * np.pi, 30)
x = np.outer (np.sin (u), np.sin (v))
y = np.outer (np.sin (u), np.cos (v))
z = np.outer (np.cos (u), np.ones_like (v))
ax.plot_wireframe (x, y, z, alpha = 0.3)

plt.show ()

在这里输入图片描述

撰写回答