Creating Various 3D Plots in Python with Matplotlib

Creating Various 3D Plots in Python with Matplotlib

Here are various examples of $3D$ $plots$ using $Python$’s $Matplotlib$ library.

These examples demonstrate how to create different types of 3D graphs, including surface plots, scatter plots, and wireframe plots.

Example 1: 3D Surface Plot

A $surface$ $plot$ is useful for visualizing a 3D surface.

Let’s plot the function:

$$
z = \sin(\sqrt{x^2 + y^2})
$$

Python Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Create grid points
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))

# Create the 3D figure
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Plot the surface
surf = ax.plot_surface(x, y, z, cmap='viridis')

# Add color bar for reference
fig.colorbar(surf)

# Set labels
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')

plt.title("3D Surface Plot")
plt.show()

Output:

Example 2: 3D Scatter Plot

A $3D$ $scatter$ $plot$ is useful for visualizing data points in three dimensions.

Python Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Generate random data
x = np.random.rand(100)
y = np.random.rand(100)
z = np.random.rand(100)

# Create the 3D figure
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Plot the scatter
ax.scatter(x, y, z, c=z, cmap='plasma')

# Set labels
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')

plt.title("3D Scatter Plot")
plt.show()

Output:

Example 3: 3D Wireframe Plot

A $wireframe$ $plot$ shows the structure of a 3D surface using lines instead of a solid surface.

Python Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Create grid points
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
x, y = np.meshgrid(x, y)
z = np.cos(np.sqrt(x**2 + y**2))

# Create the 3D figure
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Plot the wireframe
ax.plot_wireframe(x, y, z, color='blue')

# Set labels
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')

plt.title("3D Wireframe Plot")
plt.show()

Output:

Example 4: 3D Contour Plot

A $contour$ $plot$ in 3D shows contour lines (constant values) of a 3D surface.

Python Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Create grid points
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
x, y = np.meshgrid(x, y)
z = np.sin(x) * np.cos(y)

# Create the 3D figure
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Plot the contour
ax.contour3D(x, y, z, 50, cmap='coolwarm')

# Set labels
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')

plt.title("3D Contour Plot")
plt.show()

Output:

Example 5: 3D Bar Plot

A $3D$ $bar$ $plot$ is useful for displaying data across three axes.

Python Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Data for bars
_x = np.arange(4)
_y = np.arange(3)
_xx, _yy = np.meshgrid(_x, _y)
x, y = _xx.ravel(), _yy.ravel()
z = np.zeros_like(x)

# Bar heights
dz = [1, 2, 3, 4, 2, 3, 1, 4, 2, 1, 3, 4]

# Create the 3D figure
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Plot the 3D bars
ax.bar3d(x, y, z, 1, 1, dz, shade=True)

# Set labels
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')

plt.title("3D Bar Plot")
plt.show()

Output:

Conclusion

These examples show how to create various types of $3D$ $plots$ in $Python$ using $Matplotlib$.

You can customize these plots further by adjusting parameters like colors, axes labels, and grid points.

$3D$ $plotting$ is a powerful way to visualize complex data and mathematical functions in $Python$.