ogrid用切片作为下标,返回的是一组可用来广播计算的数组。其切片下标有如下形式:
1,[ 开始值:结束值:步长 ]
x,y=np.ogrid[1:4:1,1:5:2] print(x) print(y) 结果为: [[1] [2] [3]] [[1 3]]
2,[ 开始值:结束值:长度j ]
x,y=np.ogrid[1:4:3j,1:5:2j] print(x) print(y) [[ 1. ] [ 2.5] [ 4. ]] [[ 1. 5.]]
例子:绘制三维曲面f(x,y)=x*exp(-x^2-y^2)
from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt from matplotlib import cm import numpy as np x,y=np.ogrid[-2:2:20j,-2:2:20j] z=x*np.exp(-x**2-y**2) fig = plt.figure() ax = fig.gca(projection='3d') ax.plot_surface(x, y, z, alpha=0.3) ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') plt.show()
结果为: