How to plot 4D scatter-plot with custom colours and cutom area size in Python Matplotlib?

A 4D scatter plot in Matplotlib allows you to visualize four dimensions of data simultaneously: X and Y coordinates, point size (area), and color. This is useful for analyzing relationships between multiple variables in a single visualization.

Installing Matplotlib

First, install matplotlib using pip ?

pip install matplotlib

Basic 2D Scatter Plot

Let's start with a simple 2D scatter plot using tennis player statistics ?

import matplotlib.pyplot as plt

# Tennis player data (name, grand slam titles)
tennis_stats = (('Federer', 20), ('Nadal', 20), ('Djokovic', 17), ('Sampras', 14), 
                ('Emerson', 12), ('Laver', 11), ('Murray', 3), ('Wawrinka', 3),
                ('Zverev', 0), ('Thiem', 1), ('Medvedev', 0), ('Tsitsipas', 0))

titles = [title for player, title in tennis_stats]
players = [player for player, title in tennis_stats]

plt.scatter(titles, players)
plt.xlabel('Grand Slam Titles')
plt.ylabel('Tennis Players')
plt.show()

Adding Custom Colors (3rd Dimension)

Now let's add colors as a third dimension using custom color mapping ?

import matplotlib.pyplot as plt
import random

tennis_stats = (('Federer', 20), ('Nadal', 20), ('Djokovic', 17), ('Sampras', 14), 
                ('Emerson', 12), ('Laver', 11), ('Murray', 3), ('Wawrinka', 3),
                ('Zverev', 0), ('Thiem', 1), ('Medvedev', 0), ('Tsitsipas', 0))

titles = [title for player, title in tennis_stats]
players = [player for player, title in tennis_stats]

# Define custom colors
custom_colors = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#FF00FF', '#00FFFF']

# Assign random colors to each point
colors = [random.choice(custom_colors) for _ in range(len(titles))]

plt.scatter(titles, players, c=colors, alpha=0.7)
plt.xlabel('Grand Slam Titles')
plt.ylabel('Tennis Players')
plt.show()

Creating a 4D Scatter Plot

Finally, let's create a true 4D scatter plot by adding varying point sizes as the fourth dimension ?

import matplotlib.pyplot as plt
import random
import numpy as np

tennis_stats = (('Federer', 20), ('Nadal', 20), ('Djokovic', 17), ('Sampras', 14), 
                ('Emerson', 12), ('Laver', 11), ('Murray', 3), ('Wawrinka', 3),
                ('Zverev', 0), ('Thiem', 1), ('Medvedev', 0), ('Tsitsipas', 0))

titles = [title for player, title in tennis_stats]
players = list(range(len(tennis_stats)))  # Use indices for y-axis

# Third dimension: Custom colors based on titles
colors = ['red' if t > 15 else 'blue' if t > 5 else 'green' for t in titles]

# Fourth dimension: Point sizes based on titles (area)
sizes = [(t * 20 + 50) for t in titles]  # Scale sizes based on titles

plt.figure(figsize=(10, 6))
scatter = plt.scatter(titles, players, c=colors, s=sizes, alpha=0.6, edgecolors='black')

plt.xlabel('Grand Slam Titles')
plt.ylabel('Player Index')
plt.title('4D Scatter Plot: Tennis Players')

# Add player names as labels
player_names = [player for player, _ in tennis_stats]
plt.yticks(players, player_names)

plt.grid(True, alpha=0.3)
plt.show()

Complete Example with All Features

import matplotlib.pyplot as plt
import numpy as np

# Sample data for 4D visualization
np.random.seed(42)
n_points = 50

# Four dimensions of data
x = np.random.randn(n_points)           # 1st dimension
y = np.random.randn(n_points)           # 2nd dimension  
colors = np.random.rand(n_points)       # 3rd dimension (color)
sizes = 1000 * np.random.rand(n_points) # 4th dimension (size)

plt.figure(figsize=(10, 8))
scatter = plt.scatter(x, y, c=colors, s=sizes, alpha=0.6, cmap='viridis', edgecolors='black')

plt.colorbar(scatter, label='Color Dimension')
plt.xlabel('X Dimension')
plt.ylabel('Y Dimension')
plt.title('4D Scatter Plot\n(X, Y, Color, Size)')
plt.grid(True, alpha=0.3)
plt.show()

Key Parameters

The main parameters for creating 4D scatter plots are ?

  • x, y: First two dimensions (position)
  • c: Third dimension (color values or color list)
  • s: Fourth dimension (point sizes/areas)
  • alpha: Transparency (0-1)
  • cmap: Colormap for continuous color data

Conclusion

4D scatter plots in Matplotlib effectively visualize complex datasets by using position, color, and size to represent four different variables. Use transparency and edge colors to improve readability when points overlap.

Updated on: 2026-03-25T12:03:33+05:30

976 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements