给出具有7个点的3D散点图。这7个点,几何形成一个双四面体。我能够将所有底点(z = 0)连接到顶点(z = 4/3 * sqrt(3))。
底座是两个三角形,有一个共同的质心(中间点),直接位于顶点下方。
如何勾画构成基础的两个三角形?
在我的数字数组中,第一个三角形的基数为[index0,1,2],第二个三角形为[index3,4,5],[index7]为双金字塔的顶点(四面体)。
下图显示了我想看到的内容。缺少的行是红色的。
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
import math as m
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
tr = m.sqrt(3.)
sx = m.sqrt(6.)
x = np.array([4.,2.,6.,4.,6.,2.,4.])
y = np.array([1.,1.+2*tr,1.+2*tr,1+(8*tr)/3,1+(2*tr)/3,1+(2*tr)/3,1+(4*tr)/3])
z = np.array([0.,0.,0.,0.,0.,0.,(4*sx)/3])
ax.scatter(x,y,zs = z, s=100)
for r, s, t in zip(x, y, z):
X = np.array([r, 4.])
Y = np.array( [s, 1+(4*tr)/3])
Z = np.array([t, (4*sx)/3])
ax.plot3D(X, Y, Z, 'b')
ax.set_ylim([0,8])
ax.set_xlim([8,0])
ax.set_zlim([0,8])
plt.show()
答案 0 :(得分:1)
绘制缺失的顶点:
for index in range(6):
import math
next_idx = (index+1) % 3 + math.floor(index / 3) * 3
X = (x[index], x[next_idx])
Y = (y[index], y[next_idx])
Z = (z[index], z[next_idx])
ax.plot3D(X, Y, Z, 'r')
操作代码是:
next_idx = (index+1) % 3 + math.floor(index / 3) * 3
这使用了数据来自三元组的事实。它根据next index
选择current index plus one
,但保留在3组中。这有效地选择了所有可能序列中三角形的所有三个元素。
这是有效的,因为(index+1) % 3
只会采用值0, 1, 2
,而math.floor(index / 3) * 3
会采用值0, 3, 6...
。所以它最终成为:
index, next_idx
0 1
1 2
2 0
3 4
4 5
5 3
这也可以在没有循环的情况下使用list comprehension:
完成X = [x[i] for i in (0, 1, 2, 0)]
Y = [y[i] for i in (0, 1, 2, 0)]
Z = [z[i] for i in (0, 1, 2, 0)]
ax.plot3D(X, Y, Z, 'r')
X = [x[i] for i in (3, 4, 5, 3)]
Y = [y[i] for i in (3, 4, 5, 3)]
Z = [z[i] for i in (3, 4, 5, 3)]
ax.plot3D(X, Y, Z, 'r')