Best Practice to Implement 3D Scatter Plot with Matplotlib – Matplotlib Tutorial

By | July 9, 2019

Implement 2D scatter plot is very simple, how about implementing a 3D scatter plot? In this tutorial, we will introduce how to implement a 3D one.

Best Practice to Implement Scatter Plot with Matplotlib – Matplotlib Tutorial

Preliminaries

#-*- coding: UTF-8 -*- 
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

Create data (x, y, z)

x = [1, 8, 3, 4, 5, 6, 7, 2, 9, 10]
y = [1, 5, 8, 7, 10, 9, 3, 6, 2, 4]
z = [2, 3, 3, 3, 5, 7, 9, 11, 9,10]

Create 3D scatter plot

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.scatter(x, y, z)

Set axis labels

ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

Display 3D scatter plot

plt.show()

The 3D scatter plot is like:

matplotlib 3d scatter plot

Leave a Reply

Your email address will not be published. Required fields are marked *