5

I want to get 3d grid and I use the following command:


    x = np.linspace(-10,10,100)
    y = np.linspace(-10,10,100)
    z = np.linspace(-10,10,100)

    X,Y,Z = np.mgrid[x, y, z]

And such error emerges


        AttributeError                            Traceback (most recent call last)
     in ()
          6 
          7 
    ----> 8 X,Y,Z = np.mgrid[x, y, z]
          9 
         10 #X,Y,Z = np.mgrid[-10:10, -10:10, -10:10]

    ~/Desktop/PyProjects/Phys/env/lib/python3.6/site-packages/numpy/lib/index_tricks.py in __getitem__(self, key)
        165             typ = int
        166             for k in range(len(key)):
    --> 167                 step = key[k].step
        168                 start = key[k].start
        169                 if start is None:

    AttributeError: 'numpy.ndarray' object has no attribute 'step'

I know I can use this form to generate the grid


    X,Y,Z = np.mgrid[-10:10, -10:10, -10:10]

but it is not mobile for me

4
  • 3
    I believe mgrid is just a convenient way to pass slices. Normally the function is meshgrid: np.meshgrid(x, y, z). Does that work for you?
    – ayhan
    Jul 3, 2018 at 9:27
  • I am stupid because I didn't know about it. Thanks a lot) it works for me Jul 3, 2018 at 9:32
  • You should post that as an answer and accept it so that others can see it more clearly :)
    – Wool
    Jul 3, 2018 at 9:33
  • Maybe @user2285236 should do this and get all the credits) Jul 3, 2018 at 9:36

1 Answer 1

3

Try np.meshgrid() instead of mgrid:

X, Y, Z = np.meshgrid(x, y, z)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.