Create 3D Scatterplot with Python Matplotlib

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/scatter_plots("`Scatter Plots`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills python/with_statement -.-> lab-48919{{"`Create 3D Scatterplot with Python Matplotlib`"}} matplotlib/importing_matplotlib -.-> lab-48919{{"`Create 3D Scatterplot with Python Matplotlib`"}} matplotlib/figures_axes -.-> lab-48919{{"`Create 3D Scatterplot with Python Matplotlib`"}} matplotlib/scatter_plots -.-> lab-48919{{"`Create 3D Scatterplot with Python Matplotlib`"}} python/for_loops -.-> lab-48919{{"`Create 3D Scatterplot with Python Matplotlib`"}} python/lists -.-> lab-48919{{"`Create 3D Scatterplot with Python Matplotlib`"}} python/tuples -.-> lab-48919{{"`Create 3D Scatterplot with Python Matplotlib`"}} python/function_definition -.-> lab-48919{{"`Create 3D Scatterplot with Python Matplotlib`"}} python/importing_modules -.-> lab-48919{{"`Create 3D Scatterplot with Python Matplotlib`"}} python/standard_libraries -.-> lab-48919{{"`Create 3D Scatterplot with Python Matplotlib`"}} python/math_random -.-> lab-48919{{"`Create 3D Scatterplot with Python Matplotlib`"}} python/numerical_computing -.-> lab-48919{{"`Create 3D Scatterplot with Python Matplotlib`"}} python/data_visualization -.-> lab-48919{{"`Create 3D Scatterplot with Python Matplotlib`"}} end

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.

Other Python Tutorials you may like