How to make a 4D plot with Matplotlib using arbitrary data?

A 4D plot in Matplotlib uses three spatial dimensions (x, y, z) plus a fourth dimension represented by color or size. We can create this using scatter() with a 3D projection, where the fourth dimension is mapped to color values.

Basic 4D Scatter Plot

Here's how to create a 4D plot using random data points ?

import matplotlib.pyplot as plt
import numpy as np

# Set figure properties
plt.rcParams["figure.figsize"] = [10.00, 6.00]
plt.rcParams["figure.autolayout"] = True

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

# Generate random data for 4 dimensions
x = np.random.standard_normal(100)
y = np.random.standard_normal(100) 
z = np.random.standard_normal(100)
c = np.random.standard_normal(100)  # 4th dimension (color)

# Create 4D scatter plot
scatter = ax.scatter(x, y, z, c=c, cmap='viridis', alpha=0.8, s=50)

# Add labels and colorbar
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
plt.colorbar(scatter, label='4th Dimension')
plt.title('4D Plot using Color Mapping')

plt.show()

Using Size as the Fourth Dimension

Instead of color, you can use point size to represent the fourth dimension ?

import matplotlib.pyplot as plt
import numpy as np

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

# Generate data
x = np.random.randn(50)
y = np.random.randn(50)
z = np.random.randn(50)
size_data = np.random.rand(50) * 500 + 50  # 4th dimension (size)

# Create scatter plot with varying sizes
ax.scatter(x, y, z, s=size_data, c='blue', alpha=0.6)

ax.set_xlabel('X axis')
ax.set_ylabel('Y axis') 
ax.set_zlabel('Z axis')
plt.title('4D Plot using Point Size')

plt.show()

Combined Color and Size Mapping

For richer visualization, combine both color and size to represent different aspects ?

import matplotlib.pyplot as plt
import numpy as np

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

# Generate data
n_points = 100
x = np.random.randn(n_points)
y = np.random.randn(n_points) 
z = np.random.randn(n_points)
colors = np.random.rand(n_points)      # 4th dimension (color)
sizes = np.random.rand(n_points) * 200 + 20  # 5th dimension (size)

# Create enhanced 4D plot
scatter = ax.scatter(x, y, z, c=colors, s=sizes, 
                    cmap='plasma', alpha=0.7, edgecolors='black', linewidth=0.5)

ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
plt.colorbar(scatter, label='Color Dimension')
plt.title('Enhanced 4D Plot with Color and Size')

plt.show()

Key Parameters

Parameter Purpose Example Values
c Color mapping (4th dimension) Array of values
s Size mapping Array of sizes (20-500)
cmap Color scheme 'viridis', 'plasma', 'coolwarm'
alpha Transparency 0.0 to 1.0

Conclusion

4D plotting in Matplotlib maps the fourth dimension to visual properties like color or size. Use c parameter for color mapping and s for size variation. Always include colorbars and labels for clarity.

Updated on: 2026-03-25T19:52:34+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements