logo

Numpy Meshgrid funkcija

Thenumpy.meshgrid>funkcija tiek izmantota, lai izveidotu taisnstūrveida režģi no diviem dotiem viendimensijas masīviem, kas attēlo Dekarta indeksu vai matricas indeksāciju. Meshgrid funkcija ir zināmā mērā iedvesmota no MATLAB. Apsveriet tālāk redzamo attēlu ar X asi no -4 līdz 4 un Y ass diapazonu no -5 līdz 5. Tātad kopā ir (9 * 11) = 99 punkti, kas atzīmēti attēlā katrs ar X koordinātu un Y-koordināta. Jebkurai taisnei, kas ir paralēla X asij, atzīmēto punktu X koordinātas ir attiecīgi -4, -3, -2, -1, 0, 1, 2, 3, 4. No otras puses, jebkurai taisnei paralēli Y asij atzīmēto punktu Y koordinātas no apakšas uz augšu ir -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5.numpy.meshgrid>funkcija atgriež divus divdimensiju masīvus, kas attēlo visu punktu X un Y koordinātas. Piemēri:

Ievade: x = [-4, -3, -2, -1, 0, 1, 2, 3, 4] y = [-5, -4, -3, -2, -1, 0, 1, 2, 3 , četri]. Izvade: x_1 = masīvs([[-4., -3., -2., -1., 0., 1., 2., 3., 4.], [-4., -3., -2. , -1., 0., 1., 2., 3., 4.], [-4., -3., -2., -1., 0., 1., 2., 3., 4.], [-4., -3., -2., -1., 0., 1., 2., 3., 4.], [-4., -3., -2., -1., 0., 1., 2., 3., 4.], [-4., -3., -2., -1., 0., 1., 2., 3., 4 .], [-4., -3., -2., -1., 0., 1., 2., 3., 4.], [-4., -3., -2., - 1., 0., 1., 2., 3., 4.], [-4., -3., -2., -1., 0., 1., 2., 3., 4. ], [-4., -3., -2., -1., 0., 1., 2., 3., 4.], [-4., -3., -2., -1 ., 0., 1., 2., 3., 4.]]) y_1 = masīvs([[-5., -5., -5., -5., -5., -5., - 5., -5., -5.], [-4., -4., -4., -4., -4., -4., -4., -4., -4.], [-3., -3., -3., -3., -3., -3., -3., -3., -3.], [-2., -2., -2. , -2., -2., -2., -2., -2., -2.], [-1., -1., -1., -1., -1., -1. , -1., -1., -1.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 1., 1., 1., 1., 1., 1., 1., 1., 1.], [ 2., 2., 2., 2., 2., 2., 2., 2., 2.] , [ 3., 3., 3., 3., 3., 3., 3., 3., 3.], [ 4., 4., 4., 4., 4., 4., 4 ., 4., 4.], [ 5., 5., 5., 5., 5., 5., 5., 5., 5.]]) Ievade: x = [0, 1, 2, 3, 4, 5] y = [2, 3, 4, 5, 6, 7, 8] Izvade: x_1 = masīvs([[0., 1., 2., 3., 4., 5.], [0., 1., 2., 3., 4., 5.], [0., 1 ., 2., 3., 4., 5.], [0., 1., 2., 3., 4., 5.], [0., 1., 2., 3., 4. , 5.], [0., 1., 2., 3., 4., 5.], [0., 1., 2., 3., 4., 5.]]) y_1 = array( [[2., 2., 2., 2., 2., 2.], [3., 3., 3., 3., 3., 3.], [4., 4., 4. , 4., 4., 4.], [5., 5., 5., 5., 5., 5.], [6., 6., 6., 6., 6., 6.] , [7., 7., 7., 7., 7., 7.], [8., 8., 8., 8., 8., 8.]]

Zemāk ir kods:
# Sample code for generation of first example> import> numpy as np> # from matplotlib import pyplot as plt> # pyplot imported for plotting graphs> > x>=> np.linspace(>->4>,>4>,>9>)> > # numpy.linspace creates an array of> # 9 linearly placed elements between> # -4 and 4, both inclusive> y>=> np.linspace(>->5>,>5>,>11>)> > # The meshgrid function returns> # two 2-dimensional arrays> x_1, y_1>=> np.meshgrid(x, y)> > print>(>'x_1 = '>)> print>(x_1)> print>(>'y_1 = '>)> print>(y_1)>
   Output:  x_1 = [[-4. -3. -2. -1. 0. 1. 2. 3. 4.] [-4. -3. -2. -1. 0. 1. 2. 3. 4.] [-4. -3. -2. -1. 0. 1. 2. 3. 4.] [-4. -3. -2. -1. 0. 1. 2. 3. 4.] [-4. -3. -2. -1. 0. 1. 2. 3. 4.] [-4. -3. -2. -1. 0. 1. 2. 3. 4.] [-4. -3. -2. -1. 0. 1. 2. 3. 4.] [-4. -3. -2. -1. 0. 1. 2. 3. 4.] [-4. -3. -2. -1. 0. 1. 2. 3. 4.] [-4. -3. -2. -1. 0. 1. 2. 3. 4.] [-4. -3. -2. -1. 0. 1. 2. 3. 4.]] y_1 = [[-5. -5. -5. -5. -5. -5. -5. -5. -5.] [-4. -4. -4. -4. -4. -4. -4. -4. -4.] [-3. -3. -3. -3. -3. -3. -3. -3. -3.] [-2. -2. -2. -2. -2. -2. -2. -2. -2.] [-1. -1. -1. -1. -1. -1. -1. -1. -1.] [ 0. 0. 0. 0. 0. 0. 0. 0. 0.] [ 1. 1. 1. 1. 1. 1. 1. 1. 1.] [ 2. 2. 2. 2. 2. 2. 2. 2. 2.] [ 3. 3. 3. 3. 3. 3. 3. 3. 3.] [ 4. 4. 4. 4. 4. 4. 4. 4. 4.] [ 5. 5. 5. 5. 5. 5. 5. 5. 5.]]  The output of coordinates by meshgrid can also be used for plotting functions within the given coordinate range.  An Ellipse:          ellipse = xx * 2 + 4 * yy**2 plt.contourf(x_1, y_1, ellipse, cmap = 'jet')    plt.colorbar()  plt.show()       Output:  Random Data:         random_data = np.random.random((11, 9))  plt.contourf(x_1, y_1, random_data, cmap = 'jet')    plt.colorbar()  plt.show()       Output:  A Sine function:          sine = (np.sin(x_1**2 + y_1**2))/(x_1**2 + y_1**2)  plt.contourf(x_1, y_1, sine, cmap = 'jet')    plt.colorbar()  plt.show()       Output:   We observe that x_1 is a row repeated matrix whereas y_1 is a column repeated matrix. One row of x_1 and one column of y_1 is enough to determine the positions of all the points as the other values will get repeated over and over. So we can edit above code as follows: x_1, y_1 = np.meshgrid(x, y, sparse = True) This will produce the following output:  x_1 = [[-4. -3. -2. -1. 0. 1. 2. 3. 4.]] y_1 = [[-5.] [-4.] [-3.] [-2.] [-1.] [ 0.] [ 1.] [ 2.] [ 3.] [ 4.] [ 5.]] The shape of x_1 changed from (11, 9) to (1, 9) and that of y_1 changed from (11, 9) to (11, 1) The indexing of Matrix is however different. Actually, it is the exact opposite of Cartesian indexing.  For the matrix shown above, for a given row Y-coordinate increases as 0, 1, 2, 3 from left to right whereas for a given column X-coordinate increases from top to bottom as 0, 1, 2. The two 2-dimensional arrays returned from Matrix indexing will be the transpose of the arrays generated by the previous program. The following code can be used for obtaining Matrix indexing:         # Sample code for generation of Matrix indexing  import numpy as np      x = np.linspace(-4, 4, 9)  # numpy.linspace creates an array  # of 9 linearly placed elements between  # -4 and 4, both inclusive  y = np.linspace(-5, 5, 11)    # The meshgrid function returns  # two 2-dimensional arrays  x_1, y_1 = np.meshgrid(x, y)      x_2, y_2 = np.meshgrid(x, y, indexing = 'ij')    # The following 2 lines check if x_2 and y_2 are the  # transposes of x_1 and y_1 respectively  print('x_2 = ')  print(x_2)  print('y_2 = ')  print(y_2)    # np.all is Boolean and operator;  # returns true if all holds true.  print(np.all(x_2 == x_1.T))  print(np.all(y_2 == y_1.T))       Output:  x_2 = [[-4. -4. -4. -4. -4. -4. -4. -4. -4. -4. -4.] [-3. -3. -3. -3. -3. -3. -3. -3. -3. -3. -3.] [-2. -2. -2. -2. -2. -2. -2. -2. -2. -2. -2.] [-1. -1. -1. -1. -1. -1. -1. -1. -1. -1. -1.] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [ 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2.] [ 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3.] [ 4. 4. 4. 4. 4. 4. 4. 4. 4. 4. 4.]] y_2 = [[-5. -4. -3. -2. -1. 0. 1. 2. 3. 4. 5.] [-5. -4. -3. -2. -1. 0. 1. 2. 3. 4. 5.] [-5. -4. -3. -2. -1. 0. 1. 2. 3. 4. 5.] [-5. -4. -3. -2. -1. 0. 1. 2. 3. 4. 5.] [-5. -4. -3. -2. -1. 0. 1. 2. 3. 4. 5.] [-5. -4. -3. -2. -1. 0. 1. 2. 3. 4. 5.] [-5. -4. -3. -2. -1. 0. 1. 2. 3. 4. 5.] [-5. -4. -3. -2. -1. 0. 1. 2. 3. 4. 5.] [-5. -4. -3. -2. -1. 0. 1. 2. 3. 4. 5.]] True True  The sparse = True can also be added in the meshgrid function of Matrix indexing. In this case, the shape of x_2 will change from (9, 11) to (9, 1) and that of y_2 will change from (9, 11) to (1, 11).>