Programmers Picnic AI-ML Classes by Champak Roy

3D in Matplotlib

A fascinating 0 to infinity lesson on creating 3D points, lines, scatter plots, surfaces, wireframes, contours, vectors, animations, and machine learning loss landscapes.

What students will learn

  • What x, y, and z mean in 3D plotting
  • How to create 3D axes in Matplotlib
  • How to plot 3D scatter, line, surface, wireframe, and contour plots
  • How to use NumPy meshgrid
  • How to visualize ML loss surfaces
  • How to run every example inside the embedded Python editor

0. The Big Idea

In 2D plotting, every point has two values:

x, y

In 3D plotting, every point has three values:

x, y, z
X Y Z Point (x, y, z) 3D means one extra axis Matplotlib draws a 3D box where data has width, depth, and height.
The magic line is projection="3d". Without this, Matplotlib creates a normal 2D plot.

1. Setup

Install Matplotlib and NumPy:

pip install matplotlib numpy

Basic 3D setup:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(projection="3d")

plt.show()
fig = whole figure ax = drawing area projection="3d" = 3D mode

2. First 3D Point

This program places one point at x = 1, y = 2, z = 3.

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(projection="3d")

ax.scatter(1, 2, 3)

ax.set_title("My First 3D Point")
ax.set_xlabel("X axis")
ax.set_ylabel("Y axis")
ax.set_zlabel("Z axis")

plt.show()

3. 3D Line Plot

A 3D line is created by connecting many 3D points. This example creates a spiral.

import numpy as np
import matplotlib.pyplot as plt

t = np.linspace(0, 10, 200)

x = np.sin(t)
y = np.cos(t)
z = t

fig = plt.figure()
ax = fig.add_subplot(projection="3d")

ax.plot(x, y, z)

ax.set_title("3D Spiral Line")
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")

plt.show()

4. 3D Scatter Plot

A scatter plot shows individual points. This is useful for data science.

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(10)

x = np.random.rand(100)
y = np.random.rand(100)
z = np.random.rand(100)

fig = plt.figure()
ax = fig.add_subplot(projection="3d")

ax.scatter(x, y, z)

ax.set_title("3D Scatter Plot")
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")

plt.show()

5. Surface Plot

A surface plot is used when z depends on x and y.

z = f(x, y)

Example:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)

X, Y = np.meshgrid(x, y)

Z = np.sin(np.sqrt(X**2 + Y**2))

fig = plt.figure()
ax = fig.add_subplot(projection="3d")

surface = ax.plot_surface(X, Y, Z, cmap="viridis")

fig.colorbar(surface, ax=ax, shrink=0.6, label="Z value")

ax.set_title("3D Surface Plot")
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")

plt.show()
Surface plots usually need np.meshgrid(). It creates a grid of x and y values.

6. Wireframe Plot

A wireframe is like the skeleton of a surface.

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-4, 4, 60)
y = np.linspace(-4, 4, 60)

X, Y = np.meshgrid(x, y)
Z = np.cos(X) * np.sin(Y)

fig = plt.figure()
ax = fig.add_subplot(projection="3d")

ax.plot_wireframe(X, Y, Z)

ax.set_title("3D Wireframe Plot")
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")

plt.show()

7. Machine Learning Loss Surface

This is one of the most powerful uses of 3D plotting. We can show how loss changes when weight and bias change.

import numpy as np
import matplotlib.pyplot as plt

w = np.linspace(-5, 5, 100)
b = np.linspace(-5, 5, 100)

W, B = np.meshgrid(w, b)

Loss = (W - 2)**2 + (B - 1)**2

fig = plt.figure()
ax = fig.add_subplot(projection="3d")

ax.plot_surface(W, B, Loss, cmap="viridis")

ax.set_title("Machine Learning Loss Surface")
ax.set_xlabel("Weight")
ax.set_ylabel("Bias")
ax.set_zlabel("Loss")

plt.show()
The lowest point of the surface is the best solution. This is the visual idea behind gradient descent.

8. 3D Plot Types Cheat Sheet

Plot Command Best Use
3D Point / Scatter ax.scatter(x, y, z) Raw 3D data points
3D Line ax.plot(x, y, z) Paths, spirals, curves
Surface ax.plot_surface(X, Y, Z) Mathematical surfaces
Wireframe ax.plot_wireframe(X, Y, Z) Surface structure
Contour ax.contour3D(X, Y, Z) Height levels
3D Bar ax.bar3d(x, y, z, dx, dy, dz) 3D quantities
Vector ax.quiver(x, y, z, u, v, w) Direction and force

9. Embedded Python Editor

Use this embedded editor to run the Matplotlib examples. Copy any code from the lesson above and paste it inside the editor.

Python Starter Editor | Matplotlib 3D Practice
If the editor does not load inside the page because of browser or server security settings, use the Open New Tab button.

10. Student Practice Checklist

11. Final Challenge

Build one complete Matplotlib 3D demo with a menu:

1. 3D Scatter
2. 3D Line
3. 3D Surface
4. Wireframe
5. ML Loss Surface

When the user enters a number, show the selected 3D plot.