My Coding >
Programming language >
Python >
Python libraries and packages >
Python NumPy >
NumPy: What the difference between mgrid and meshgrid
NumPy: What the difference between mgrid and meshgridNumPy mgrid and meshgrid are, in fact, doing the same things. They generate Ndimensional coordinate grid for Ndimensional field calculations. In this short article, I will show you, how you can use these functions and what is the difference between them. I will also will give an idea, how to read them from external programs. I’ve already use meshgrid function in some calculations, see for example: Electric field potential and intensity (Python visualization of electric field), Interactive Electric field with Mathplotlib (Interactive Electric Field) and Continuous Electric field lines (Drawing continuous Electric field lines). Also mgrid was described: 3D Electric field with Mayavi and the same with visual explanation: Field Lines with Mayavi. Despite such extensive usage of these two functions with very similar properties, I do not explain, what they doing and what is the difference between mgrid and meshgrid In all further examples, I will assume, than NumPy and matplotlib are already loaded
NumPy mgrid syntaxTo generate spatial coordinate grid with mgrid function from NumPy it is necessary to know the start and end points in every axis and also number of steps or step size. mgrid with stepsize
This function will create X and Y two dimensional arrays with shape 5, 4 containing:
As you can see, the high range is not included. mgrid with number of stepsAnother way of using mgrid, is to give number of steps as an imaginary value. But in this case, the high border will be included. To make almost the same coordinate grid we should use the following command:
Results will be the same in shape but slightly different in the type of arrays. In this case it will be float.
It is important to remember about these tiny differences. NumPy meshgrid syntaxMeshgrid is more similar to mgris syntax with imaginary number of steps, but you should provide list of grids. This can be useful for nonlinear or any kind of irregular grid points. For linear grid, it is very convenient to use numpy linspace to generate grid points towards each axis. So let’s generate the same 2dimensional grid:
And the result will be:
As you can see, the data generally the same, but the orientation looks different How to use mgrid and meshgrid coordinate gridpointsBoth mgrid and meshgrid generating arrays with dots in every grid of our spatial domain. By joining X and Y arrays you can make a pair of coordinates for every gridpoint:
You can plot these points for 2 or 3 dimensional cases:
You can easily check, that meshgrid will produce the same gridponts. How to use gridpoints coordinate arraysGenerally speaking, if you have a NumPy array with coordinates, you can apply any function to it and calculate array with values in these gridpoints. Or, for more detailed exmples, please go to check our article about Electric field potential and intensity calculations What is the difference between mgrid and meshgridFor two dimensional case you can change between mgrid and meshgrid structure by transposing one of them. In higher dimensional case the procedure will be slightly different. So let’s imagine, that X, Y is a mgrid structure and Xm, Ym  meshgrid respectively. Converting between 2D casesFor two dimensional case matrix transpose will do the job. To compare NumPy structures, which should be identical I do prefer to use array_equal function from NumPy
Converting in 3DcaseI will generate structures, then check the shape and then do conversion
As you can see, the difference is only in the order of first two axes (axis0 and axis1). To make them identical, it is necessary to swat these two axis with command swapaxes
Converting between mgrid and meshgrid in Ndimensional spaceBearing in mind, that in twodimensional case, the transposition of matrices are equal to swapaxes(0, 1) it is possible to predict, that in any dimensional case, command swapaxes(0, 1) will convert from mgrid and meshgrid and vice versa.

Last 10 artitles
9 popular artitles


© 2020 MyCoding.uk My blog about coding and further learning. This blog was writen with pure Perl and frontend output was performed with TemplateToolkit. 