2017-03-05 2 views
2

보톰은 0 또는 1 중 하나를 포함하는 N_1 x N_2 x N_3 행렬이 주어 졌을 때 1시 위치에 volumic 픽셀 (복셀)이있는 N_1 x N_2 x N_3 볼륨으로 데이터를 3D로 표시 할 방법을 찾고있었습니다. 1의 좌표가 [[1, 1, 1], [4, 1, 2], [3, 4, 1]] 인 경우matplotlib로 복셀 표현하기

예를 들어, 원하는 출력이

하기 matplotlib의 mplot3D 모듈이이를 달성 할 수있는 잠재력을 가질 수있는 것

, 그러나 나는 천국과 같을 것이다 이런 종류의 음모에 대한 예를 발견하지 못했습니다. 누구든지이 문제를 해결할 수있는 간단한 해결책을 알고 있습니까?

도와 주셔서 감사합니다 사전에 많은.

답변

2

this answer (부분적으로는 this answer을 기준으로 함)의 코드를 적용하면 입방 형을 쉽게 surface plots으로 표시 할 수 있습니다.

그러면 입력 배열을 반복 할 수 있고 이 배열 인덱스에 해당하는 위치에 직사각형을 그립니다.

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

def cuboid_data(pos, size=(1,1,1)): 
    # code taken from 
    # https://stackoverflow.com/a/35978146/4124317 
    # suppose axis direction: x: to left; y: to inside; z: to upper 
    # get the (left, outside, bottom) point 
    o = [a - b/2 for a, b in zip(pos, size)] 
    # get the length, width, and height 
    l, w, h = size 
    x = [[o[0], o[0] + l, o[0] + l, o[0], o[0]], 
     [o[0], o[0] + l, o[0] + l, o[0], o[0]], 
     [o[0], o[0] + l, o[0] + l, o[0], o[0]], 
     [o[0], o[0] + l, o[0] + l, o[0], o[0]]] 
    y = [[o[1], o[1], o[1] + w, o[1] + w, o[1]], 
     [o[1], o[1], o[1] + w, o[1] + w, o[1]], 
     [o[1], o[1], o[1], o[1], o[1]],   
     [o[1] + w, o[1] + w, o[1] + w, o[1] + w, o[1] + w]] 
    z = [[o[2], o[2], o[2], o[2], o[2]],      
     [o[2] + h, o[2] + h, o[2] + h, o[2] + h, o[2] + h], 
     [o[2], o[2], o[2] + h, o[2] + h, o[2]],    
     [o[2], o[2], o[2] + h, o[2] + h, o[2]]]    
    return x, y, z 

def plotCubeAt(pos=(0,0,0),ax=None): 
    # Plotting a cube element at position pos 
    if ax !=None: 
     X, Y, Z = cuboid_data(pos) 
     ax.plot_surface(X, Y, Z, color='b', rstride=1, cstride=1, alpha=1) 

def plotMatrix(ax, matrix): 
    # plot a Matrix 
    for i in range(matrix.shape[0]): 
     for j in range(matrix.shape[1]): 
      for k in range(matrix.shape[2]): 
       if matrix[i,j,k] == 1: 
        # to have the 
        plotCubeAt(pos=(i-0.5,j-0.5,k-0.5), ax=ax)    

N1 = 10 
N2 = 10 
N3 = 10 
ma = np.random.choice([0,1], size=(N1,N2,N3), p=[0.99, 0.01]) 

fig = plt.figure() 
ax = fig.gca(projection='3d') 
ax.set_aspect('equal') 

plotMatrix(ax, ma) 

plt.show() 

enter image description here

+0

이 신속하고 간결한 응답을 보내 주셔서 감사합니다. – Learn12

+0

이 기능을 matplotlib에'ax.voxels (matrix)'로 추가하는 [PR here] (https://github.com/matplotlib/matplotlib/pull/6404)를 만들었습니다. 내부면을 생략하는 기능이 있습니다. – Eric

2

upcomming하기 matplotlib 버전 2.1은 examples for 3D voxels 기능을 가지고있다.

아나콘다를 사용하는 경우 conda-forge 채널을 통해 설치할 수 있습니다.

conda install -c conda-forge matplotlib