What is the purpose of meshgrid in Python / NumPy?

前端 未结 7 1951
渐次进展
渐次进展 2020-12-04 04:03

Can someone explain to me what is the purpose of meshgrid function in Numpy? I know it creates some kind of grid of coordinates for plotting, but I can\'t reall

7条回答
  •  情歌与酒
    2020-12-04 04:51

    Short answer

    The purpose of meshgrid is to help remplace Python loops (slow interpreted code) by vectorized operations within C NumPy library.

    Borrowed from this site.

    x = np.arange(-4, 4, 0.25)
    y = np.arange(-4, 4, 0.25)
    X, Y = np.meshgrid(x, y)
    R = np.sqrt(X**2 + Y**2)
    Z = np.sin(R)
    

    meshgrid is used to create pairs of coordinates between -4 and +4 with .25 increments in each direction X and Y. Each pair is then used to find R, and Z from it. This way of preparing "a grid" of coordinates is frequently used in plotting 3D surfaces, or coloring 2D surfaces.


    Details: Python for-loop vs NumPy vector operation

    To take a more simple example, let's say we have two sequences of values,

    a = [2,7,9,20]    
    b = [1,6,7,9]    ​
    

    and we want to perform an operation on each possible pair of values, one taken from the first list, one taken from the second list. We also want to store the result. For example, let's say we want to get the sum of the values for each possible pair.

    Slow and laborious method

    c = []    
    for i in range(len(b)):    
        row = []    
        for j in range(len(a)):    
            row.append (a[j] + b[i])
        c.append (row)    
    print (c)
    

    Result:

    [[3, 8, 10, 21],
     [8, 13, 15, 26],
     [9, 14, 16, 27],
     [11, 16, 18, 29]]
    

    Python is interpreted, these loops are relatively slow to execute.

    Fast and easy method

    meshgrid is intended to remove the loops from the code. It returns two arrays (i and j below) which can be combined to scan all the existing pairs like this:

    i,j = np.meshgrid (a,b)    
    c = i + j    
    print (c)
    

    Result:

    [[ 3  8 10 21]
     [ 8 13 15 26]
     [ 9 14 16 27]
     [11 16 18 29]]
    

    Meshgrid under the hood

    The two arrays prepared by meshgrid are:

    (array([[ 2,  7,  9, 20],
            [ 2,  7,  9, 20],
            [ 2,  7,  9, 20],
            [ 2,  7,  9, 20]]),
    
     array([[1, 1, 1, 1],
            [6, 6, 6, 6],
            [7, 7, 7, 7],
            [9, 9, 9, 9]]))
    

    These arrays are created by repeating the values provided. One contains the values in identical rows, the other contains the other values in identical columns. The number of rows and column is determined by the number of elements in the other sequence.

    The two arrays created by meshgrid are therefore shape compatible for a vector operation. Imagine x and y sequences in the code at the top of page having a different number of elements, X and Y resulting arrays will be shape compatible anyway, not requiring any broadcast.

    Origin

    numpy.meshgrid comes from MATLAB, like many other NumPy functions. So you can also study the examples from MATLAB to see meshgrid in use, the code for the 3D plotting looks the same in MATLAB.

提交回复
热议问题