# How to make an ellipse-shaped MeshGrid using NumPy

## 1. Introduction

Python’s NumPy library is one of the many tools available that make Python one of the most powerful languages in the world. Python is ranked in the top 5 programming languages no matter where you look, and this is in part because of the wide variety of uses it has in spheres ranging from website development to Big Data to Artificial Intelligence.

NumPy, as a tool, is used across most of these disciplines for its ability to manipulate data on a grand scale. It is for this purpose that my class used it to picture 3-dimensional shapes like the one above. However, when challenged to make a shape that we all recognize, like the classic Pringle-Chip, we were at a loss.

After some head scratching, this is the code used to create the shape pictured above:

The NumPy library is used to generate and manipulate the multi-dimensional data arrays. MatPlotLib is a rendering engine used to generate the images seen.

## 2. Explanation

Now that the code is available, let's get a closer look at exactly what is going on here.

`r = np.linspace(0, 1, 100)`

theta = np.linspace(0, 2*np.pi, 100)

r, theta = np.meshgrid(r, theta)

When making a mesh, since Cartesian Coordinates are used by NumPy’s plotting functions, we are comfortable building array’s in terms of X and Y, but this ends up creating a rectangular mesh. Some shapes, like that above, are better understood with a round mesh. This code, like that from a regular mesh, creates a pair of 2-dimensional arrays, one of radii values and one of angles.

This is where the helper functions come in, with a circular mesh created, the stretch_mesh function is used to adjust the values of the radius arrays stretching and compressing them into an ellipse.

`r= stretch_mesh(r, theta)`

The stretch_mesh function does not edit the angle array, so only the new radius array is returned. After this, the last step is to transform the data from Polar Coordinates into Cartesian Coordinates.

`X = r * np.sin(theta)`

Y = r * np.cos(theta)

These X and Y arrays function like a normal MeshGrid, with the exception of their distinct shape.

## 3. Improvements

Now that the instructions have been laid out, a discussion of improvements is in order. While this code is functional, there are several improvements that could be made to this and other similar models for plotting unique shapes.

First, the current algorithm stretches the MeshGrid using *x* and *y* arguments which come directly from the standard cartesian definition of an eclipse. Replacing this with an equation in terms of polar coordinates would generate a more elegant and consistent solution.

Second, this algorithm is only able to create an ellipse. With some further development, generating other conic sections would bring even greater freedom for plotting surfaces.

## 4. Conclusions

Python, Numpy, and NumPlotLib are powerful tools that have the ability to go far beyond my math homework. The graphics used in this article have been generated using python, and range from 600 to 10000 data points, stretched and adjusted to fit the needs and whims of individuals, and while these arrays are useful for visualizing data, they are also, in my opinion, quite beautiful.