# How to Create a 3D Scatterplot in Matplotlib

Often you may want to create a 3D scatterplot in Matplotlib to visualize the relationship between three different variables.

This can be useful in a variety of situations where creating a simple 2D scatterplot to visualize the relationship between two variables is not sufficient.

You can use the following basic syntax to create a 3D scatterplot in Matplotlib:

import matplotlib.pyplot as plt

#initialize figure
fig = plt.figure()

#define values for scatterplot
x_values = [1, 2, 2, 3, 4, 4, 5, 7, 8, 9]
y_values = [2, 4, 4, 3, 5, 7, 6, 7, 9, 9]
z_values = [8, 8, 7, 6, 7, 5, 4, 4, 5, 2]

#create 3D scatterplot
ax.scatter(x_values, y_values, z_values)
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('Z-axis')

#ensure that all axis labels are shown
ax.set_box_aspect(None, zoom=0.85)

#display 3D scatterplot
plt.show()

This particular example will create a 3D scatterplot in Matplotlib that allows us to visualize the points from the arrays named x_values, y_values and z_values.

Note: If you leave out ax.set_box_aspect() it’s possible that one or more of the axis labels will be cut off in the plot. By using this function with the argument zoom=0.85 we ensure that all axis labels are visible.

The following example shows how to use this syntax in practice.

## Example: How to Create a 3D Scatterplot in Matplotlib

Suppose that we would like to create a 3D scatterplot to visualize the relationship between the following three variable for various basketball players:

• points
• assists
• rebounds

We can use the following syntax to do so:

import matplotlib.pyplot as plt

#initialize figure
fig = plt.figure()

#define values for scatterplot
points = [1, 2, 2, 3, 4, 4, 5, 7, 8, 9]
assists = [2, 4, 4, 3, 5, 7, 6, 7, 9, 9]
rebounds = [8, 8, 7, 6, 7, 5, 4, 4, 5, 2]

#create 3D scatterplot
ax.scatter(points, assists, rebounds)
ax.set_xlabel('Points')
ax.set_ylabel('Assists')
ax.set_zlabel('Rebounds')

#ensure that all axis labels are shown
ax.set_box_aspect(None, zoom=0.85)

#display 3D scatterplot
plt.show()

This syntax produces the following 3D scatterplot in Matplotlib:

The x-axis displays the points, the y-axis displays the assists and the z-axis displays the rebounds.

Note that you can also use the following arguments within the ax.scatter() function to customize how the points look in the plot:

• s: The size of the points in the plot (larger values = larger points)
• c: The color of the points in the plot
• marker: The shape of the points in the plot

For example, we can use the following syntax to generate the same 3D scatterplot except with larger points that are red and shaped like triangles:

import matplotlib.pyplot as plt

#initialize figure
fig = plt.figure()

#define values for scatterplot
points = [1, 2, 2, 3, 4, 4, 5, 7, 8, 9]
assists = [2, 4, 4, 3, 5, 7, 6, 7, 9, 9]
rebounds = [8, 8, 7, 6, 7, 5, 4, 4, 5, 2]

#create 3D scatterplot
ax.scatter(points, assists, rebounds, s=30, c='red', marker='v')
ax.set_xlabel('Points')
ax.set_ylabel('Assists')
ax.set_zlabel('Rebounds')

#ensure that all axis labels are shown
ax.set_box_aspect(None, zoom=0.85)

#display 3D scatterplot
plt.show()

This syntax produces the following 3D scatterplot in Matplotlib:

Notice that the points in the plot are now customized exactly how we specified within the ax.scatter() function.

In this particular example we chose to use an upside down arrow as the marker shape but you can find a complete list of available marker shapes here.

Note: You can find the complete documentation for the ax.scatter() function in Matplotlib here.