In this tutorial, we’ll try to understand how to plot a 3D plot in python.

Matplotlib was initially designed with only two-dimensional plotting in mind. Around the time of the 1.0 release, some three-dimensional plotting utilities were built on top of Matplotlib’s two-dimensional display, and the result is a convenient (if somewhat limited) set of tools for three-dimensional data visualization. three-dimensional plots are enabled by importing the mplot3d toolkit.

## Using Axes3D from mplot3d Toolkit

- We can plot a 3-dimensional plot in python using mplot3d toolkit.
- To plot a 3D plot we need three-dimensional axes that can be created by passing
`projection='3d'`

to any of the normal axes (matplotlib`Axes`

object). - For the examples in this tutorial, we create 3D Axes (of class Axes3D) by passing the
`projection="3d"`

keyword argument to Figure.add_subplot.

In simple terms, when we provide the `projection='3D'`

parameter to the `Figure.add_subplot`

method, we’re trying to generate 3D axes of class Axes3D that can be used to plot any three-dimensional figure.

Now let us understand the above method using some examples.

### Example 1 – Plot 3D axes without any data

import matplotlib.pyplot as plt fig = plt.figure(figsize=(4,4)) ax = fig.add_subplot(111, projection='3d')

Output:

The steps followed in the above example are:

- import required modules
- generate a 3D axes using
`figure.add_subplot(projection='3d')`

### Example 2 – Plot a point in the 3D axes

import matplotlib.pyplot as plt fig = plt.figure(figsize=(4,4)) ax = fig.add_subplot(111, projection='3d') ax.scatter(0,0,0) #plotting a point at (0,0,0) coordinate plt.show()

Output:

The steps followed in the above example are:

- import required modules
- generate a 3D axes using
`figure.add_subplot(projection='3d')`

- Plot a point using
`axes.scatter`

method.

### Example 3 – Plot a continuous curve in the 3D axes

import matplotlib.pyplot as plt import numpy as np fig = plt.figure(figsize=(4,4)) ax = fig.add_subplot(111, projection='3d') z = np.linspace(0, 1, 100) x = z * np.sin(20 * z) y = z * np.cos(20 * z) ax.plot3D(x, y, z, 'gray') plt.show()

Output:

The steps followed in the above example are:

- import required modules
- generate a 3D axes using
`figure.add_subplot(projection='3d')`

- define the variables for 3 different variables
- generate z values using
`numpy.linspace`

method (refer this).generate x values using`x = zsin(20z)`

generate y values using`y = zcos(20z)`

- generate z values using
- plot the 3d plot using
`axes.plot3D`

### Example 4 – Create a 3D scatter plot with customizations

import matplotlib.pyplot as plt fig = plt.figure(figsize=(4,4)) ax = fig.add_subplot(111, projection='3d') x = np.random.random(100)*10+20 y = np.random.random(100)*5+7 z = np.random.random(100)*15+50 ax.scatter(x, y, z, 'gray') plt.show()

Output:

The steps followed in the above example are:

- import required modules
- generate a 3D axes using
`figure.add_subplot(projection='3d')`

- define the variables for 3 different variables
- scatter plot the 3d plot using
`axes.scatter`

### Example 5 – Add axes labels and plot title to a 3D plot

import matplotlib.pyplot as plt fig = plt.figure(figsize=(7,7)) ax = fig.add_subplot(111, projection='3d') x = np.random.random(100)*10+20 y = np.random.random(100)*5+7 z = np.random.random(100)*15+50 ax.scatter(x, y, z, 'gray') ax.set_title("3D plot") ax.set_xlabel("X axes") ax.set_ylabel("Y axes") ax.set_zlabel("Z axes") plt.show()

Output:

The steps followed in the above example are:

- import required modules
- generate a 3D axes using
`figure.add_subplot(projection='3d')`

- define the variables for 3 different variables
- scatter plot the 3d plot using
`axes.scatter`

- Set the title and axes labels.

### Example 6 – Customize markers in a 3D scatter plot

import matplotlib.pyplot as plt fig = plt.figure(figsize=(7,7)) ax = fig.add_subplot(111, projection='3d') x = np.random.random(100)*10+20 y = np.random.random(100)*5+7 z = np.random.random(100)*15+50 ax.scatter(x, y, z, color='red',marker='x') plt.show()

Output:

The steps followed in the above example are:

- import required modules
- generate a 3D axes using
`figure.add_subplot(projection='3d')`

- define the variables for 3 different variables
- scatter plot the 3d plot using
`axes.scatter`

by modifying the markers.

### Example 7 – Plot a 3d polygon

import matplotlib.pyplot as plt from mpl_toolkits.mplot3d.art3d import Poly3DCollection fig = plt.figure(figsize=(7,7)) ax = fig.add_subplot(111, projection='3d') x = [1, 0, 3, 4] y = [0, 5, 5, 1] z = [1, 3, 4, 0] vertices = [list(zip(x,y,z))] poly = Poly3DCollection(vertices, alpha=0.8) ax.add_collection3d(poly) ax.set_xlim(0,5) ax.set_ylim(0,5) ax.set_zlim(0,5)

Output:

The steps followed in the above example are:

- import required modules
- generate a 3D axes using
`figure.add_subplot(projection='3d')`

- define the variables for 3 different variables
- Make a 3d polygon collection using
`Poly3DCollection`

and add the collection to the axes

You might also be interested in –

- How to plot a 3D surface plot in Python?
- How to Plot a 3D Contour plot in Python?
- How to plot a Quiver plot in Python?

**Subscribe to our newsletter for more informative guides and tutorials. ****We do not spam and you can opt out any time.**