2014-12-03 4 views
6

3D 점 구름 (점의 수 : N)을 그리고 점에서 볼록한 선체 (사실 N 개의 꼭짓점이있는 다면체)를 그려야합니다. scipy.spatial ConvexHull을 사용하여 파이썬으로 스크립트를 만들었습니다. 플롯 8 점에 대해 점을 찍었고 점 구름의 플롯은 괜찮습니다. 그러나 코드는 두 줄을 큐브의 대각선면을 가로 질러 놓기 때문에 괜찮지 않습니다. 가장자리 선 이외에. 왜 얼굴에 줄거리가 있는지 이해할 수 없습니다.점 구름에서 3D 볼록 선체

스크립트 : 스크립트의

import numpy as np 
from mpl_toolkits.mplot3d import Axes3D 
import matplotlib.pyplot as plt 
from scipy.spatial import ConvexHull 

fig = plt.figure() 
ax = fig.add_subplot(111, projection='3d') 

points= np.array([[0,0,0], 
      [4,0,0], 
      [4,4,0], 
      [0,4,0], 
      [0,0,4], 
      [4,0,4], 
      [4,4,4], 
      [0,4,4]]) 

hull=ConvexHull(points) 

edges= zip(*points) 

for i in hull.simplices: 
    plt.plot(points[i,0], points[i,1], points[i,2], 'r-') 

ax.plot(edges[0],edges[1],edges[2],'bo') 

ax.set_xlabel('x') 
ax.set_ylabel('y') 
ax.set_zlabel('z') 

ax.set_xlim3d(-5,5) 
ax.set_ylim3d(-5,5) 
ax.set_zlim3d(-5,5) 

plt.show() 

결과 :

enter image description here

+0

내 질문은 어떻게 편집합니까? 첫 번째 행에서 "Hi All"을 잊어 버렸습니다. "edit"를 클릭하고 변경 사항을 저장하려면 아무 일도 일어나지 않습니다. – Feri

답변

3

나는이 오래 알지만, 그래서 다른 사람도 있다고 생각 여기에 구글에서왔다.

문제는 사용하는 플로팅 방법에만 있습니다. 하나의 심플 렉스는 3 점으로 정의되는 nD 삼각형입니다. 그러나 플롯 기능은 마지막 포인트로 되돌아 가야합니다. 그렇지 않으면 3 개의 단색 가장자리 중 2 개만 그려집니다.

import numpy as np 
import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D 
from scipy.spatial import ConvexHull 


# 8 points defining the cube corners 
pts = np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], 
       [0, 0, 1], [1, 0, 1], [1, 1, 1], [0, 1, 1], ]) 

hull = ConvexHull(pts) 

fig = plt.figure() 
ax = fig.add_subplot(111, projection="3d") 

# Plot defining corner points 
ax.plot(pts.T[0], pts.T[1], pts.T[2], "ko") 

# 12 = 2 * 6 faces are the simplices (2 simplices per square face) 
for s in hull.simplices: 
    s = np.append(s, s[0]) # Here we cycle back to the first coordinate 
    ax.plot(pts[s, 0], pts[s, 1], pts[s, 2], "r-") 

# Make axis label 
for i in ["x", "y", "z"]: 
    [enter image description here][1]eval("ax.set_{:s}label('{:s}')".format(i, i)) 

plt.show() 
관련 문제