我想在3D图中表示一个二进制3D矩阵(如果可能的话,不使用mayavi.mlab)。在矩阵为1的每个位置(x,y,z)都应绘制一个点。
我的矩阵是通过以下方式构建的:

import numpy as np
size = 21
my_matrix = np.zeros(shape = (size, size, size))
random_location_1 = (1,1,2)
random_location_2 = (3,5,8)
my_matrix[random_location_1] = 1
my_matrix[random_location_2] = 1


现在,在坐标(1,1,2)和(3,5,8)处应该可见一个点,其他任何地方都只有空白。
有什么方法可以做到这一点(例如使用matplotlib吗?)

最佳答案

听起来您需要散点图。看一下this mplot3d教程。对我来说,这工作:

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


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

size = 21
m = np.zeros(shape = (size, size, size))
random_location_1 = (1,1,2)
random_location_2 = (3,5,8)
m[random_location_1] = 1
m[random_location_2] = 1

pos = np.where(m==1)
ax.scatter(pos[0], pos[1], pos[2], c='black')
plt.show()

关于python - 在python中绘制二进制3D矩阵,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42829643/

10-16 07:09