我如何使用 mplot3d 和 numpy 数组绘制 3D 网格?

How could I plot 3D grid using mplot3d and numpy array?

我正在尝试绘制一个具有整数值的 3D NumPy 数组。

import numpy as np

def build(self):
    grid = np.empty((10,10,10))
    grid = grid.astype(np.int)
    grid.fill(-1)
    return grid

前面的方法构建了一个3D网格并填充了-1值。在这种情况下,-1 表示空单元格。

在这个网格中放置 3 个元素,例如,在位置 (0,0,0), (0,0,1) y (5,5,5) 之后我看不到相同数量的元素阴谋。我在这里附上一张图片

图像右侧有第四个元素(色调较小)。

我绘制网格的代码如下:

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

def plot_grid(self, np_grid):
        fig = plt.figure()
        ax = fig.add_subplot(111, projection='3d')
        x = np_grid.sum(0)
        y = np_grid.sum(1)
        z = np_grid.sum(2)
        ax.scatter(x, y, -z, zdir='z', c='red')
        plt.show()

我想我没有正确获取 x,y,z 矢量。我怎样才能正确地从 ndarray 得到 x,y,z

我认为您的问题是您需要将 x、y 和 z 作为它们自己的数组传递:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter((0,0,5),(0,0,5),(0,1,5),zdir='z', c='red')

这应该给你一个这样的情节: