Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy: get the squares coordinates in the mesh grid

Tags:

python

numpy

mesh

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)
like image 486
JuanPablo Avatar asked Jan 22 '26 14:01

JuanPablo


2 Answers

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.

like image 198
wim Avatar answered Jan 25 '26 02:01

wim


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

like image 29
Jose Avatar answered Jan 25 '26 03:01

Jose



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!