我希望将坐标网格的 x 和 y 值放入它们自己单独的数组中,以执行毕达哥拉斯等函数。 下面是我的代码。
x1d = np.linspace(-xlen,xlen,res)
y1d = np.linspace(-ylen,ylen,res)
from itertools import product
coordinates = list(product(x1d, y1d))
xcoord = coordinates[:][:][0]
print np.shape(coordinates), np.shape(xcoord), coordinates
我知道下面的代码会给我
coordinates = [[x1,y1],[x2,y2],...,[xn,yn]].
如何提取以下数组?
xcoord = [x1,x2,...,xn]
ycoord = [x1,x2,...,xn]
这是生成二维点网格的正确解决方案吗?我可以在其中对每个单独的 x,y 点执行函数,并为该点分配一个结果值?
谢谢!
最佳答案
您还可以使用 itertools 获取 x 和 y 值:
import itertools
x,y=itertools.izip(*coordinates)
# x=(x1,x2,...,xn)
# y=(y1,y2,...,yn)
关于网格,看看 numpy 的 meshgrid这可能对你有用。您可以像这样使用它(取自链接网站上的示例):
x=np.arange(-5,5,.1)
y=np.arange(-5,5,.1)
xx,yy=meshgrid(x,y,sparse=True)
xx,yy=np.meshgrid(x,y,sparse=True)
z = np.sin(xx**2 + yy**2) / (xx**2 + yy**2)
h = plt.contourf(x,y,z)
关于python - 访问多维数组中的坐标 - python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20259575/