Introduction
This tutorial will guide you on how to create a 3D scatterplot using Python's Matplotlib library. The scatterplot is a graphical representation of the relationship between three variables. It is a useful tool for identifying patterns and trends in complex data.
VM Tips
After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.
Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.
If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.
Import the necessary libraries
To create a 3D scatterplot, we will be using the Matplotlib library. We will also be using the NumPy library to generate random data.
import matplotlib.pyplot as plt
import numpy as np
Set up the data
We will generate two sets of data with random values using the NumPy library. One set will represent the x and y coordinates, and the other set will represent the z coordinate.
def randrange(n, vmin, vmax):
"""
Helper function to make an array of random numbers having shape (n, )
with each number distributed Uniform(vmin, vmax).
"""
return (vmax - vmin)*np.random.rand(n) + vmin
n = 100
for m, zlow, zhigh in [('o', -50, -25), ('^', -30, -5)]:
xs = randrange(n, 23, 32)
ys = randrange(n, 0, 100)
zs = randrange(n, zlow, zhigh)
Create the figure and subplot
We will create the figure and subplot using the add_subplot function from the Matplotlib library. We will also set the projection to '3d' to create a 3D plot.
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
Create the scatterplot
We will create the scatterplot using the scatter function from the Matplotlib library. We will pass in the x, y, and z coordinates as well as the marker style.
ax.scatter(xs, ys, zs, marker=m)
Set the axis labels
We will set the labels for the x, y, and z axes using the set_xlabel, set_ylabel, and set_zlabel functions from the Matplotlib library.
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
Display the plot
We will display the plot using the show function from the Matplotlib library.
plt.show()
Summary
In this tutorial, we learned how to create a 3D scatterplot using the Matplotlib library in Python. We set up the data using the NumPy library, created the figure and subplot using the add_subplot function, created the scatterplot using the scatter function, set the axis labels using the set_xlabel, set_ylabel, and set_zlabel functions, and displayed the plot using the show function. With these skills, you can create and customize 3D scatterplots to analyze and visualize complex data.