JuanPablo
JuanPablo

Reputation: 24764

numpy: get the squares coordinates in the mesh grid

In python, with numpy, I can generate a meshgrid

import numpy as np
import matplotlib.pyplot as plt

def main():
    x = np.linspace(0, 10, 4)
    y = np.linspace(0, 10, 4)

    x,y = np.meshgrid(x,y)
    print x,y

if __name__ == '__main__':
    main()

and I get:

[[  0.           3.33333333   6.66666667  10.        ]
 [  0.           3.33333333   6.66666667  10.        ]
 [  0.           3.33333333   6.66666667  10.        ]
 [  0.           3.33333333   6.66666667  10.        ]]
[[  0.           0.           0.           0.        ]
 [  3.33333333   3.33333333   3.33333333   3.33333333]
 [  6.66666667   6.66666667   6.66666667   6.66666667]
 [ 10.          10.          10.          10.        ]]

how I can get the elements (squares) of the meshgrid, with the vertex ?

example: the square upper-left have the vertexs

(0, 0)   (0,3.3)
(3.3,0)  (3.3, 3.3)

Upvotes: 3

Views: 1890

Answers (3)

Jose
Jose

Reputation: 11

It's a bit late by a way for that is as followsnp.hstack((x.reshape((x.size,1)),y.reshape((y.size,1))))

Upvotes: 1

Midnighter
Midnighter

Reputation: 3881

Check out the documentation for numpy.ix_.

Upvotes: -1

wim
wim

Reputation: 362617

I believe that would be

np.dstack([x,y])[row:row+2, col:col+2, :]

Where row, col are the coordinates of the top-left corner for that square. Won't work on the last row or column, obviously.

Upvotes: 3

Related Questions