Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Matplotlib 3D Plotting - Line and Scatter Plot

In this tutorial, we will cover Three Dimensional Plotting in the Matplotlib.

It is important to note that Matplotlib was initially designed with only two-dimensional plotting in mind. But later on, some three-dimensional plotting utilities were built on top of Matplotlib's two-dimensional display, which provides a set of tools for three-dimensional data visualization in matplotlib.

Also, a 2D plot is used to show the relationships between a single pair of axes that is x and y whereas the 3D plot, on the other hand, allows us to explore relationships of 3 pairs of axes that is x-y, x-z, and y-z

Three Dimensional Plotting

The 3D plotting in Matplotlib can be done by enabling the utility toolkit. The utility toolkit can be enabled by importing the mplot3d library, which comes with your standard Matplotlib installation via pip.

After importing this sub-module, 3D plots can be created by passing the keyword projection="3d" to any of the regular axes creation functions in Matplotlib.

Let us cover some examples for three-dimensional plotting using this submodule in matplotlib.

3D Line Plot

Here is the syntax to plot the 3D Line Plot:

Axes3D.plot(xs, ys, *args, **kwargs)

With the code snippet given below we will cover the 3D line plot in Matplotlib:

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

fig = plt.figure()
ax = plt.axes(projection='3d')
z = np.linspace(0, 1, 100)
x = z * np.sin(30 * z)
y = z * np.cos(30 * z)

ax.plot3D(x, y, z, 'maroon')
ax.set_title('3D line plot')

Following is the output for it:

3d line plot example matplotlib

3D Scatter Plot

Here is the syntax for 3D Scatter Plot:

Axes3D.scatter(xs, ys, zs=0, zdir='z', s=20, c=None, depthshade=True, *args, **kwargs)


Argument Description
xs, ys These two arguments indicate the position of data points.
zs It can be Either an array of the same length as xs and ys or it can be a single value to place all points in the same plane. The default value of this argument is 0.
zdir This Argument is used to indicate which direction to use as z (‘x’, ‘y’ or ‘z’) at the time of plotting a 2D set.
s This argument is used to indicate the Size in points. It can either be a scalar or an array of the same length as x and y.
c This argument is used to indicate the color.
depthshade This argument is used to tell Whether or not to shade the scatter markers in order to give the appearance of depth. The default value of this argument is True.

With the code snippet given below we will cover the 3D Scatter plot in Matplotlib:

fig = plt.figure()
ax = plt.axes(projection="3d")

z_line = np.linspace(0, 15, 500)
x_line = np.cos(z_line)
y_line = np.sin(z_line)
ax.plot3D(x_line, y_line, z_line, 'blue')

z_points = 15 * np.random.random(500)
x_points = np.cos(z_points) + 0.1 * np.random.randn(500)
y_points = np.sin(z_points) + 0.1 * np.random.randn(500)
ax.scatter3D(x_points, y_points, z_points, c=z_points, cmap='hsv');

The output is:

scatter 3D plot example matplotlib


In this tutorial we learned the basics of 3D plotting in Matplotlib and how we do it for Line and Scatter plot with code examples.