2016-11-03 1 views

답변

2

이것은 아주 좋은 질문을 제기 나타냅니다. matplotlib은 그러한 함수를 직접 제공하지 않지만 6면으로 큐브를 시뮬레이션 할 수 있습니다. 이 서피스를 가져 오면 here 코드를 사용할 수 있습니다. 그런 다음 행렬에 정의 된 위치에 큐브를 플롯해야합니다. 플롯을 등각 투영으로 보이게하기 위해 mpl3d 축의 입방 경계 상자 모서리에 보이지 않는 점을 플로팅하여 해결 방법을 사용합니다. 마지막으로 축을 보이지 않게해야합니다.

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

def cuboid_data(center, size=(1,1,1)): 
    # code taken from 
    # https://stackoverflow.com/questions/30715083/python-plotting-a-wireframe-3d-cuboid?noredirect=1&lq=1 
    # 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(center, size)] 
    # get the length, width, and height 
    l, w, h = size 
    x = [[o[0], o[0] + l, o[0] + l, o[0], o[0]], # x coordinate of points in bottom surface 
     [o[0], o[0] + l, o[0] + l, o[0], o[0]], # x coordinate of points in upper surface 
     [o[0], o[0] + l, o[0] + l, o[0], o[0]], # x coordinate of points in outside surface 
     [o[0], o[0] + l, o[0] + l, o[0], o[0]]] # x coordinate of points in inside surface 
    y = [[o[1], o[1], o[1] + w, o[1] + w, o[1]], # y coordinate of points in bottom surface 
     [o[1], o[1], o[1] + w, o[1] + w, o[1]], # y coordinate of points in upper surface 
     [o[1], o[1], o[1], o[1], o[1]],   # y coordinate of points in outside surface 
     [o[1] + w, o[1] + w, o[1] + w, o[1] + w, o[1] + w]] # y coordinate of points in inside surface 
    z = [[o[2], o[2], o[2], o[2], o[2]],      # z coordinate of points in bottom surface 
     [o[2] + h, o[2] + h, o[2] + h, o[2] + h, o[2] + h], # z coordinate of points in upper surface 
     [o[2], o[2], o[2] + h, o[2] + h, o[2]],    # z coordinate of points in outside surface 
     [o[2], o[2], o[2] + h, o[2] + h, o[2]]]    # z coordinate of points in inside surface 
    return x, y, z 

def plotCubeAt(pos=(0,0), N=0, ax=None): 
    # Plotting N cube elements at position pos 
    if ax !=None: 
     if N > 0: 
      for n in range(N): 
       X, Y, Z = cuboid_data((pos[0],pos[1],n)) 
       ax.plot_surface(X, Y, Z, color='b', rstride=1, cstride=1, alpha=1) 

def plotIsoMatrix(ax, matrix): 
    # plot a Matrix 
    # where matrix[i,j] cubes are added at position (i,j) 
    for i in range(matrix.shape[0]): 
      for j in range(matrix.shape[1]): 
       plotCubeAt(pos=(i,j), N=matrix[i,j], ax=ax) 

    l = max(matrix.shape[0], matrix.shape[1], matrix.max()) 
    bb = np.array([(0,0,0), (0,l,0), (l,0,0), (l,l,0),(0,0,l), (0,l,l), (l,0,l), (l,l,l)]) 
    ax.plot(bb[:,0], bb[:,1], bb[:,2], "w", alpha=0.0)    



if __name__ == '__main__': 
    fig = plt.figure() 
    ax = fig.gca(projection='3d') 
    ax.set_aspect('equal') 
    matrix = np.array([[3,2],[1,1]]) 
    plotIsoMatrix(ax, matrix) 
    ax.set_axis_off() 
    plt.show() 

enter image description here

+0

감사 요 대단히 –

관련 문제