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?

Upvotes: 0

Views: 1238

Answers (1)

erasmortg
erasmortg

Reputation: 3278

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:

enter image description here

Upvotes: 1

Related Questions