简体   繁体   中英

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

I am trying to plot a 3D NumPy Array which has integer values.

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. In this case, -1 is to representation of empty cell.

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. 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. How could I get x,y,z from ndarray correctly?

I believe your problem is that you need to pass the x, y, and z as arrays of their own:

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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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