Course contentsShow
← Data Science
Lesson 1324 of 2,14533. Advanced Visualization TechniquesFree lesson

3D Scatter Plots and Line Plots

Creating 3D scatter plots and line plots to visualize relationships among three continuous variables.

3D Scatter Plots and Line Plots

You'll learn how to create three-dimensional scatter and line plots to reveal relationships among three continuous variables simultaneously.

The Core Idea

While 2D plots show relationships between two variables (like height vs. weight), 3D plots add a third dimension to visualize how three continuous variables interact. Imagine plotting temperature, humidity, and air pressure together—each data point now exists in 3D space with x, y, and z coordinates.

Think of it like upgrading from a flat map to a physical globe: you gain depth perception that can reveal patterns invisible in flat projections.

3D Scatter Plots

3D scatter plots extend regular scatter plots by adding a z-axis. Each point floats in three-dimensional space, showing how three variables relate. This is ideal for exploring clusters, outliers, or trends across three dimensions simultaneously.

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
z = [1, 4, 9, 16, 25]

ax.scatter(x, y, z, c='blue', marker='o')
ax.set_xlabel('Variable X')
ax.set_ylabel('Variable Y')
ax.set_zlabel('Variable Z')
plt.show()

3D Line Plots

3D line plots connect points in three-dimensional space with lines, perfect for showing trajectories, paths, or continuous trends through 3D space (like tracking an airplane's flight path with longitude, latitude, and altitude).

ax.plot(x, y, z, color='red', linewidth=2)

When to Use Them

Use 3D plots when three variables genuinely interact and the depth dimension adds insight. However, be cautious—3D plots can be harder to read than 2D alternatives and lose clarity when printed or viewed from poor angles.

Key Takeaway: 3D scatter and line plots visualize relationships among three continuous variables by mapping data to x, y, and z coordinates, but should be used judiciously since they can reduce readability compared to simpler 2D visualizations.