1. 程式人生 > >numpy.meshgrid()理解

numpy.meshgrid()理解

本文的目的是記錄meshgrid()的理解過程:

step1. 通過一個示例引入建立網格點矩陣;

step2. 基於步驟1,說明meshgrid()的作用;

step3. 詳細解讀meshgrid()的官網定義;

說明:step1和2 的資料都是基於笛卡爾座標系的矩陣,目的是為了方便討論。

step1. 通過一個示例引入建立網格點矩陣;

示例1,建立一個2行3列的網格點矩陣。

#!/usr/bin/env python3
#-*- coding:utf-8 -*-
############################
#File Name: meshgrid1.py
#Brief:
#Author: frank
#Mail: 
[email protected]
#Created Time:2018-06-14 21:33:14 ############################ import numpy as np import matplotlib.pyplot as plt X = np.array([[0, 0.5, 1],[0, 0.5, 1]]) print("X的維度:{},shape:{}".format(X.ndim, X.shape)) Y = np.array([[0, 0, 0],[1, 1, 1]]) print("Y的維度:{},shape:{}".format(Y.ndim, Y.shape)) plt.plot(X, Y, 'o--') plt.grid(True) plt.show()

X矩陣是:[[0. 0.5 1. ], [0. 0.5 1. ]]

Y矩陣是:[[0 0 0],[1 1 1]]

step2. meshgrid()的作用;

當要描繪的 矩陣網格點的資料量小的時候,可以用上述方法構造網格點座標資料;

但是如果是一個(256, 100)的整數矩陣網格,要怎樣構造資料呢?

方法1:將x軸上的100個整數點組成的行向量,重複256次,構成shape(256,100)的X矩陣;將y軸上的256個整數點組成列向量,重複100次構成shape(256,100)的Y矩陣

顯然方法1的資料構造過程很繁瑣,也不方便呼叫,那麼有沒有更好的辦法呢?of course!!!

那麼meshgrid()就顯示出它的作用了

使用meshgrid方法,你只需要構造一個表示x軸上的座標的向量和一個表示y軸上的座標的向量;然後作為引數給到meshgrid(),該函式就會返回相應維度的兩個矩陣;

例如,你想構造一個2行3列的矩陣網格點,那麼x生成一個shape(3,)的向量,y生成一個shape(2,)的向量,將x,y傳入meshgrid(),最後返回的X,Y矩陣的shape(2,3)

示例2,使用meshgrid()生成step1中的網格點矩陣

x = np.array([0, 0.5, 1])
y = np.array([0,1])

xv,yv = np.meshgrid(x, y)
print("xv的維度:{},shape:{}".format(xv.ndim, xv.shape))
print("yv的維度:{},shape:{}".format(yv.ndim, yv.shape))

plt.plot(xv, yv, 'o--')
plt.grid(True)
plt.show()

 示例3,生成一個20行30列的網格點矩陣

x = np.linspace(0,500,30)
print("x的維度:{},shape:{}".format(x.ndim, x.shape))
print(x)
y = np.linspace(0,500,20)
print("y的維度:{},shape:{}".format(y.ndim, y.shape))
print(y)

xv,yv = np.meshgrid(x, y)
print("xv的維度:{},shape:{}".format(xv.ndim, xv.shape))
print("yv的維度:{},shape:{}".format(yv.ndim, yv.shape))

plt.plot(xv, yv, '.')
plt.grid(True)
plt.show()

step3. 詳細解讀meshgrid()的官網定義;

numpy.meshgrid(*xi, **kwargs)
# Return coordinate matrices from coordinate vectors.
# 根據輸入的座標向量生成對應的座標矩陣
Parameters:
  x1, x2,…, xn : array_like
    1-D arrays representing the coordinates of a grid.
  indexing : {‘xy’, ‘ij’}, optional
    Cartesian (‘xy’, default) or matrix (‘ij’) indexing of output. See Notes for more details.
  sparse : bool, optional
    If True a sparse grid is returned in order to conserve memory. Default is False.
  copy : bool, optional
    If False, a view into the original arrays are returned in order to conserve memory. 
    Default is True. Please note that sparse=False, copy=False will likely return non-contiguous arrays. 
    Furthermore, more than one element of a broadcast array may refer to a single memory location. 
    If you need to write to the arrays, make copies first.
Returns:
  X1, X2,…, XN : ndarray
    For vectors x1, x2,…, ‘xn’ with lengths Ni=len(xi) , 
    return (N1, N2, N3,...Nn) shaped arrays if indexing=’ij’ 
    or (N2, N1, N3,...Nn) shaped arrays if indexing=’xy’ 
    with the elements of xi repeated to fill the matrix along the first dimension for x1, the second for x2 and so on.

針對indexing引數的說明:

indexing只是影響meshgrid()函式返回的矩陣的表示形式,但並不影響座標點

x = np.array([0, 0.5, 1])
y = np.array([0,1])

xv,yv = np.meshgrid(x, y)
print("xv的維度:{},shape:{}".format(xv.ndim, xv.shape))
print("yv的維度:{},shape:{}".format(yv.ndim, yv.shape))
print(xv)
print(yv)

plt.plot(xv, yv, 'o--')
plt.grid(True)
plt.show()

x = np.array([0, 0.5, 1])
y = np.array([0,1])

xv,yv = np.meshgrid(x, y,indexing='ij')
print("xv的維度:{},shape:{}".format(xv.ndim, xv.shape))
print("yv的維度:{},shape:{}".format(yv.ndim, yv.shape))
print(xv)
print(yv)

plt.plot(xv, yv, 'o--')
plt.grid(True)
plt.show()