0

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?

1
  • The approach here is not understandable. Please provide a minimal reproducible example (including what "(0,0,0), (0,0,1) y (5,5,5)" should do), then clearly explain what should happen. Mind that already grid.fill(-1) will simply put -1 in the array everywhere and summing over the first axis will hence give you 10*(-1) everywhere. Commented Oct 22, 2017 at 14:10

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Unless I misunderstood the question, if so, please clarify
Thanks for your reply. You understood well my question. My mistake was I did not know how to build x, y and z vectors. With your answer, you have illustrated how I can do it. I have to take the coordinates of the element that I want to print by separate vectors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.