3D Parametrics with Python and MatPlotLib
By: Lior Ben-David
Tags: Math, Parametric, Python, Matplotlib, Quick

Before we can begin coding, we first have to install MatPlotLib, which we can do with pip

python -m pip install -U matplotlib

MatPlotLib is an easy to use the library which has various tools to help visualize 2D and 3D data.

At the top of our file, we are going to import all the necessary tools from MatPlotLib, as well as NumPy, a mathematics library that comes installed with MatPlotLib.

import matplotlib.pyplot as plt

from mpl_toolkits import mplot3d

import numpy as np

Now let’s set up the 3D Graphing. MatPlotLib makes this incredibly easy. First, we are going to create the axes of our graph, which we will specify to be 3D

ax = plt.axes(projection = '3d')

For the sake of working our way up, we are just going to be graphing a couple of points in 3D space. To graph these points, we are going to make an array of coordinates for each dimension

x = [0, 1, 2]

y = [3, 4, 4]

z = [1, 2, 3]

This is going to store 3 points: (0, 3, 1), (1, 4, 2), and (2, 4,3). MatPlotLib will iterate through the arrays, picking the corresponding coordinates from the 3 arrays.

Finally, to plot the values along our axes we are going to do:

ax.scatter(x,y,z)

and to create a window to show us our beautiful graph:

plt.show()

If we run this Python script we should see a window with the following scatterplot appear:

Now notice that this only graphs the points we specified, but does not draw a curve between them. To draw a curve between them. We are going to replace our ax.scatter() function with:

ax.plot(x,y,z)

We then see:

Before we continue, our code at this point is:

import matplotlib.pyplot as plt

from mpl_toolkits import mplot3d

import numpy as np ax = plt.axes(projection = '3d') x = [0, 1, 2]

y = [3, 4, 4]

z = [1, 2, 3] ax.plot(x,y,z)

plt.show()

Easy enough! Now let’s graph something more interested. To get started on creating our parametric we are going to import the same things and create our axes the same way. Before we define our x,y, and z arrays though, we need to create our independent variable, t, which the x,y, and z coordinates will be functions of.

For our first example we are going to graph the parametric function:

We are going to create our t values using NumPy’s linspace() function which gives us a set of values between bounds that we can specify

t = np.linspace(0,90,100)

This will create an array, t, which has 100 evenly spaced out values between 0 and 90.

Next, let's define our x, y, and z values in terms of our parameter t:

x = np.sin(t / 5)

y = np.cos(t / 5)

z = 2 * t + 5

We now have our x, y, and z arrays that MatPlotLib will use to plot the parametric. After adding the two functions to plot and show the graph we end up with:

import matplotlib.pyplot as plt

from mpl_toolkits import mplot3d

import numpy as np ax = plt.axes(projection = '3d') t = np.linspace(0,90,100) x = np.sin(t / 5)

y = np.cos(t / 5)

z = 2 * t + 5 ax.plot(x,y,z)

plt.show()

Running this we get:

And there’s our spiral!

To make any parametric simply swap out the x, y, and z definitions with the functions you want!

Here are some other cool curves you can make with 3D parametrics:

x = ln(t), y = sin(t), z = tcos(t)