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()

이 신속하고 간결한 응답을 보내 주셔서 감사합니다. – Learn12
이 기능을 matplotlib에'ax.voxels (matrix)'로 추가하는 [PR here] (https://github.com/matplotlib/matplotlib/pull/6404)를 만들었습니다. 내부면을 생략하는 기능이 있습니다. – Eric