简体   繁体   English

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

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

I am trying to plot a 3D NumPy Array which has integer values. 我正在尝试绘制具有整数值的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

The previous method builds a 3D grid and fills with -1 value. 先前的方法将构建3D网格并以-1值填充。 In this case, -1 is to representation of empty cell. 在这种情况下,-1表示空白单元格。

Putting 3 elements in this grid, for example, in the positions (0,0,0), (0,0,1) y (5,5,5) I do not see same number of elements after plot. 在网格中放置3个元素,例如,在(0,0,0),(0,0,1)y(5,5,5)的位置上,绘制后我看不到相同数量的元素。 I attach an image here 我在这里附上图片 .

There is a fourth element on the right of the image (with minor tonality). 图像的右边有第四个元素(具有较小的色调)。

My code to plot the grid is as given below: 我绘制网格的代码如下所示:

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

I think I am not getting x,y,z vectors correctly. 我认为我没有正确获得x,y,z向量。 How could I get x,y,z from ndarray correctly? 我怎样才能正确地从ndarray获得x,y,z

I believe your problem is that you need to pass the x, y, and z as arrays of their own: 我相信您的问题是您需要将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')

This should give you a plot like this: 这应该给你这样的情节:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM