np.meshgrid()
np.meshgrid从坐标向量返回坐标矩阵。
将两个向量拉成一个矩阵数组:
In [18]: x = np.array([0,1,2])
In [19]: y = np.array([4,5,6,7])
In [20]: xs,ys = np.meshgrid(x,y)
In [21]: xs
Out[21]:
array([[0, 1, 2],
[0, 1, 2],
[0, 1, 2],
[0, 1, 2]])
In [22]: ys
Out[22]:
array([[4, 4, 4],
[5, 5, 5],
[6, 6, 6],
[7, 7, 7]])
- 画一个灰度图,这个十分好玩
#uses Ipython
In [24]: points = np.arange(-5,5,0.01)
In [25]: xs,ys = np.meshgrid(points,points)
In [26]: import matplotlib.pyplot as plt
In [33]: a = xs ** 2 + ys ** 2
In [35]: plt.imshow(np.sqrt(a),cmap = plt.cm.gray);plt.colorbar();plt.show()
参考资料:
- numpy之meshgrid和where
- 《利用python进行数据分析》
2018.5.23