np.meshgrid()
np.meshgrid從坐標(biāo)向量返回坐標(biāo)矩陣窍荧。
將兩個(gè)向量拉成一個(gè)矩陣數(shù)組:
meshgrid.png
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]])
- 畫(huà)一個(gè)灰度圖炫贤,這個(gè)十分好玩
#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()
grey-degree.png
參考資料:
- numpy之meshgrid和where
- 《利用python進(jìn)行數(shù)據(jù)分析》
2018.5.23