Generic selectors
Exact matches only
Search in title
Search in content
wb_sunny

3-Dimensional Plots in Python Using Matplotlib

3d Plots

Just like 2-Dimenstional plots you can also create 3-Dimensional plots in Python using matplotlib. In this tutorial, we will learn how to plot 3-Dimensional plots using matplotlib.

How to Plot 3-Dimensional Plots in Python?

We will be using the mplot3d toolkit along with the matpotlib library. The mplot3d toolkit is built upon the matplotlib library to make it easy to create 3-Dimensional plots.

So without any further delay, let’s get started!

1. Import the necessary modules

To begin with, we will import matplotlib and the mplot3d toolkit. Along with these two, we will also import numpy for creating sample data. The code for importing these three is given below.

from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt

2. Create three-dimensional axes

Now we can create three-dimensional axes using the imported modules.

from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt

#create 3d axes
fig = plt.figure()
ax = plt.axes(projection='3d')
plt.show()

Output:

Axes
Axes

Now that we have the axes, let’s try plotting something. While plotting we need to make sure that we provide values for all the three ( x,y and z) axes.

In the following sections, we will learn how to make a spiral using sinusoidal functions(sine and cosine).

Before that we will learn how to add a title to the plot.

3. Adding a title to the plot

You can add a title to your plots using the set_title() method:

ax.set_title('Learning about 3D plots') 

To see the above line of code in action, run the following :

from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt

#create 3d axes
fig = plt.figure()
ax = plt.axes(projection='3d')

#set title
ax.set_title('Learning about 3D plots') 

plt.show()

Output :

Plot With Title
Plot With Title

4. Create a spiral

To create a spiral we will use sine function along the x-axis and cosine function along the y-axis.

The data-points for a spiral can be generated as follows:

z = np.linspace(0, 15, 1000)
x = np.sin(z)
y = np.cos(z)

Here the function np.linespace gives 1000 equally spaced points between 0 and 15.

The complete code is as follows:

from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt

#create 3d axes
fig = plt.figure()
ax = plt.axes(projection='3d')

#cordiates for spiral
z = np.linspace(0, 15, 1000)
x = np.sin(z)
y = np.cos(z)
ax.plot3D(x, y, z, 'red')

plt.show()

Output :

Spiral
Spiral

5. Change the viewing angle

3-Dimensional plots look different depending on the viewing angle. You can change the viewing angle of the 3-Dimensional plots using the view_init() method:

ax.view_init(60, 50)

The complete code is given below:

from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt

#create 3d axes
fig = plt.figure()
ax = plt.axes(projection='3d')

#cordiates for spiral
z = np.linspace(0, 15, 1000)
x = np.sin(z)
y = np.cos(z)
ax.plot3D(x, y, z, 'red')

ax.view_init(60, 50)
plt.show()

Output :

Changing Viewing Angle
Changing Viewing Angle

Here we mention two arguments, the elevation and the angle of the axes(in degrees).

Let’s try another angle.

from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt

#create 3d axes
fig = plt.figure()
ax = plt.axes(projection='3d')

#cordiates for spiral
z = np.linspace(0, 15, 1000)
x = np.sin(z)
y = np.cos(z)
ax.plot3D(x, y, z, 'red')

ax.view_init(120, 90)
plt.show()

Output :

Example 2
Example 2

6. Plotting a wire-frame

You can plot a 3-Dimensional wireframe using the plot_wireframe() method as shown in the below example:

from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt

#create 3d axes
fig = plt.figure()
ax = plt.axes(projection='3d')

#function for Z values
def f(x, y): 
    return np.cos(np.sqrt(x ** 2 + y ** 2)) 
  
# x and y values
x = np.linspace(1, 10, 10) 
y = np.linspace(1, 10, 10) 
   
X, Y = np.meshgrid(x, y) 
Z = f(X, Y) 
 
ax = plt.axes(projection ='3d') 
ax.plot_wireframe(X, Y, Z, color ='red') 

plt.show()

Output :

Wireframe
Wireframe

Here the function np.meshgrid creates coordinate matrices from coordinate vectors.

Similarly, you can also create a surface plot. Let’s learn how to do that in the next section.

7. Create a surface plot

We can create a surface plot with the same data as above. To create a 3-Dimensional surface plot, we’ll use the plot_surface() method.

from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt

#create 3d axes
fig = plt.figure()
ax = plt.axes(projection='3d')

#function for Z values
def f(x, y): 
    return np.cos(np.sqrt(x ** 2 + y ** 2)) 
  
# x and y values 
x = np.linspace(1, 10, 10) 
y = np.linspace(1, 10, 10) 
  
X, Y = np.meshgrid(x, y) 
Z = f(X, Y) 
  
ax = plt.axes(projection ='3d') 
ax.plot_surface(X, Y, Z, rstride=1, cstride=1,
                cmap='viridis')

plt.show()

Output :

Surfaceplot
Surface-plot

Here, the following arguments mean the following :

rstrideArray row stride (step size)
cstrideArray column stride (step size)
campA colourmap for the surface patches.

Conclusion

This tutorial was about 3-Dimensional plots in Python. We learned how to plot the 3-Dimensional axes along with data-points. To learn about more 3-Dimensional shapes under mplot3d, refer to their official documentation.