Python 3.x 如何使用matplotlib或/和plotly offline在3D中可视化由顶点定义的多面体?

Python 3.x 如何使用matplotlib或/和plotly offline在3D中可视化由顶点定义的多面体?,python-3.x,matplotlib,3d,plotly-python,Python 3.x,Matplotlib,3d,Plotly Python,我想可视化3个多面体,由3个形状为8,3的numpy数组定义 我正在寻找这样的东西: 我的数据如下: A = np.array([[0.92523719, 0.26843252, 0.77794309], [0.73156748, 0.27794309, 0.57476281], [0.62113842, 0.37886158, 0.87886158], [0.72205691, 0.07476281, 0.76843252], [0.57

我想可视化3个多面体,由3个形状为8,3的numpy数组定义

我正在寻找这样的东西:

我的数据如下:

A = np.array([[0.92523719, 0.26843252, 0.77794309],
       [0.73156748, 0.27794309, 0.57476281],
       [0.62113842, 0.37886158, 0.87886158],
       [0.72205691, 0.07476281, 0.76843252],
       [0.57476281, 0.23156748, 0.72205691],
       [0.77794309, 0.42523719, 0.73156748],
       [0.87886158, 0.12113842, 0.62113842],
       [0.76843252, 0.22205691, 0.92523719]])

B = np.array([[0.23156748, 0.72205691, 0.57476281],
       [0.26843252, 0.77794309, 0.92523719],
       [0.12113842, 0.62113842, 0.87886158],
       [0.22205691, 0.92523719, 0.76843252],
       [0.27794309, 0.57476281, 0.73156748],
       [0.37886158, 0.87886158, 0.62113842],
       [0.07476281, 0.76843252, 0.72205691],
       [0.42523719, 0.73156748, 0.77794309]])

C = np.array([[0.73156748, 0.77794309, 0.42523719],
       [0.62113842, 0.87886158, 0.12113842],
       [0.77794309, 0.92523719, 0.26843252],
       [0.57476281, 0.73156748, 0.27794309],
       [0.87886158, 0.62113842, 0.37886158],
       [0.72205691, 0.57476281, 0.23156748],
       [0.76843252, 0.72205691, 0.07476281],
       [0.92523719, 0.76843252, 0.22205691]])

在3D中似乎有点,但没有边或多边形信息。假设多面体是凸的,scipy.spatial的凸包可以找到凸壳上的所有多边形。凸面外壳包含可以作为Poly3DCollection添加到3D打印的三角形

假设每个三角形的最长边是立方体的面对角线,我们可以搜索两条最短边并用黑色绘制:

从mpl_toolkits.mplot3d.art3d导入Poly3DCollection 从mpl_toolkits.mplot3d导入Axes3D 从matplotlib导入pyplot作为plt 将numpy作为np导入 从scipy.spatial导入CONVERXHULL,距离 A=np.数组[[0.92523719,0.26843252,0.77794309],[0.73156748,0.27794309,0.57476281],[0.62113842,0.37886158,0.87886158],[0.72205691,0.0747281,0.76843252],[0.57476281,0.23156748,0.72205691],[0.77794309,0.42523719,0.7315648],[0.87615842,0.113622],[0.1382727272622],] B=np.数组[[0.23156748,0.72205691,0.57476281],[0.26843252,0.77794309,0.92523719],[0.12113842,0.62113842,0.87886158],[0.22205691,0.92523719,0.76843252],[0.27794309,0.57476281,0.73156748],[0.37886158,0.87886158,0.62113842],[0.07477676281,0.72727272722],[0.732579729] C=np.数组[[0.73156748,0.77794309,0.42523719],[0.62113842,0.87886158,0.12113842],[0.77794309,0.92523719,0.26843252],[0.57476281,0.73156748,0.27794309],[0.87886158,0.62113842,0.37886158],[0.72205691,0.57476281,0.23156748],[0.76842,0.07427272727272720] 图=plt.figure ax=图添加子图111,投影=3d 对于立方体,zip中的颜色[A,B,C],'r','g','B']: 外壳=凸形外壳立方体 对于hull.simplices中的s: tri=Poly3DCollectioncube[s] 三色 三组字母0.5 三、设置边缘颜色“无” ax.add_collection3dtri 边=[] 如果距离.euclideancube[s[0]],立方体[s[1]<距离.euclideancube[s[1]],立方体[s[2]]: 边。追加[0],s[1] 如果距离.euclideancube[s[1]],立方体[s[2]<距离.euclideancube[s[2]],立方体[s[0]]: 边。附加[1],s[2] 其他: 边。追加[2],s[0] 其他: 边。附加[1],s[2] 如果距离.euclideancube[s[0]],立方体[s[1]<距离.euclideancube[s[2]],立方体[s[0]]: 边。追加[0],s[1] 其他: 边。追加[2],s[0] 对于边缘中的v0、v1: ax.plotxs=cube[[v0,v1],0],ys=cube[[v0,v1],1],zs=cube[[v0,v1],2],color='black' ax.scattercube[:,0],cube[:,1],cube[:,2],marker='o',color='purple' 节目
在3D中似乎有点,但没有边或多边形信息。假设多面体是凸的,scipy.spatial的凸包可以找到凸壳上的所有多边形。凸面外壳包含可以作为Poly3DCollection添加到3D打印的三角形

假设每个三角形的最长边是立方体的面对角线,我们可以搜索两条最短边并用黑色绘制:

从mpl_toolkits.mplot3d.art3d导入Poly3DCollection 从mpl_toolkits.mplot3d导入Axes3D 从matplotlib导入pyplot作为plt 将numpy作为np导入 从scipy.spatial导入CONVERXHULL,距离 A=np.数组[[0.92523719,0.26843252,0.77794309],[0.73156748,0.27794309,0.57476281],[0.62113842,0.37886158,0.87886158],[0.72205691,0.0747281,0.76843252],[0.57476281,0.23156748,0.72205691],[0.77794309,0.42523719,0.7315648],[0.87615842,0.113622],[0.1382727272622],] B=np.数组[[0.23156748,0.72205691,0.57476281],[0.26843252,0.77794309,0.92523719],[0.12113842,0.62113842,0.87886158],[0.22205691,0.92523719,0.76843252],[0.27794309,0.57476281,0.73156748],[0.37886158,0.87886158,0.62113842],[0.07477676281,0.72727272722],[0.732579729] C=np.数组[[0.73156748,0.77794309,0.42523719],[0.62113842,0.87886158,0.12113842],[0.77794309,0.92523719,0.26843252],[0.57476281,0.73156748,0.27794309],[0.87886158,0.62113842,0.37886158],[0.72205691,0.57476281,0.23156748],[0.76842,0.07427272727272720] 图=plt.figure ax=图添加子图111,投影=3d 对于立方体,zip中的颜色[A,B,C],'r','g','B']: 外壳=凸形外壳立方体 对于hull.simplices中的s: tri=Poly3DCollectioncube[s] 三色 三组字母0.5 三、设置边缘颜色“无” ax.add_collection3dtri 边=[] 如果距离.euclideancube[s[0]],立方体[s[1]<距离.euclideancube[s[1]],立方体[s[2]]: 边。追加[0],s[1] 如果距离.euclideancube[s[1]],立方体[s[2]<距离.euclideancube[s[2]],立方体[s[0]]: 边。附加[1],s[2] 其他: 边。追加[2],s[0] 其他: 边。附加[1],s[2] 如果距离.euclideancube[s[0]],立方体[s[1]<距离.euclideancube[s[2]],立方体[s[0]]: 边。追加[0],s[1] 其他: 边。追加[2],s[0] 对于边缘中的v0、v1: ax.plotxs=cube[[v0,v1],0],ys=cube[[v0,v1],1],zs=cube[[v0,v1],2],color='black' ax.scattercube[:,0],cube[:,1],cube[:,2],marker='o',color='purple' 节目
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import pyplot as plt
import numpy as np
from scipy.spatial import ConvexHull

A = np.array([[0.92523719, 0.26843252, 0.77794309], [0.73156748, 0.27794309, 0.57476281], [0.62113842, 0.37886158, 0.87886158], [0.72205691, 0.07476281, 0.76843252], [0.57476281, 0.23156748, 0.72205691], [0.77794309, 0.42523719, 0.73156748], [0.87886158, 0.12113842, 0.62113842], [0.76843252, 0.22205691, 0.92523719]])
B = np.array([[0.23156748, 0.72205691, 0.57476281], [0.26843252, 0.77794309, 0.92523719], [0.12113842, 0.62113842, 0.87886158], [0.22205691, 0.92523719, 0.76843252], [0.27794309, 0.57476281, 0.73156748], [0.37886158, 0.87886158, 0.62113842], [0.07476281, 0.76843252, 0.72205691], [0.42523719, 0.73156748, 0.77794309]])
C = np.array([[0.73156748, 0.77794309, 0.42523719], [0.62113842, 0.87886158, 0.12113842], [0.77794309, 0.92523719, 0.26843252], [0.57476281, 0.73156748, 0.27794309], [0.87886158, 0.62113842, 0.37886158], [0.72205691, 0.57476281, 0.23156748], [0.76843252, 0.72205691, 0.07476281], [0.92523719, 0.76843252, 0.22205691]])
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
for cube, color in zip([A, B, C], ['r', 'g', 'b']):
    hull = ConvexHull(cube)
    # draw the polygons of the convex hull
    for s in hull.simplices:
        tri = Poly3DCollection(cube[s])
        tri.set_color(color)
        tri.set_alpha(0.5)
        ax.add_collection3d(tri)
    # draw the vertices
    ax.scatter(cube[:, 0], cube[:, 1], cube[:, 2], marker='o', color='purple')
plt.show()