Scatter plot and Color mapping in Python

We can create scatter plots with color mapping using Matplotlib's scatter() method. This technique allows us to visualize an additional dimension of data through color variations, making patterns and relationships more apparent.

Basic Scatter Plot with Color Mapping

Here's how to create a scatter plot where each point has a different color based on its position in the dataset ?

import matplotlib.pyplot as plt
import numpy as np

# Generate random data points
x = np.random.rand(100)
y = np.random.rand(100)

# Create scatter plot with color mapping
colors = range(100)
plt.scatter(x, y, c=colors, cmap='viridis')
plt.colorbar()  # Add color bar to show the mapping
plt.title('Scatter Plot with Color Mapping')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.show()

Using Different Color Maps

Matplotlib provides various color maps. Here are some popular options ?

import matplotlib.pyplot as plt
import numpy as np

# Generate sample data
x = np.random.rand(50)
y = np.random.rand(50)
colors = np.random.rand(50)

# Create subplots with different colormaps
fig, axes = plt.subplots(2, 2, figsize=(10, 8))

# Different colormaps
cmaps = ['viridis', 'plasma', 'coolwarm', 'spring']
titles = ['Viridis', 'Plasma', 'Coolwarm', 'Spring']

for i, (cmap, title) in enumerate(zip(cmaps, titles)):
    row, col = i // 2, i % 2
    scatter = axes[row, col].scatter(x, y, c=colors, cmap=cmap)
    axes[row, col].set_title(title)
    plt.colorbar(scatter, ax=axes[row, col])

plt.tight_layout()
plt.show()

Color Mapping Based on Data Values

You can map colors based on actual data values rather than just position ?

import matplotlib.pyplot as plt
import numpy as np

# Generate data where color represents a third dimension
x = np.random.rand(100)
y = np.random.rand(100)
# Color based on distance from origin
colors = np.sqrt(x**2 + y**2)

plt.scatter(x, y, c=colors, cmap='hot', alpha=0.7)
plt.colorbar(label='Distance from Origin')
plt.title('Color Mapped by Distance from Origin')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.show()

Customizing Marker Size and Color

You can vary both size and color of markers simultaneously ?

import matplotlib.pyplot as plt
import numpy as np

# Generate sample data
n_points = 100
x = np.random.rand(n_points)
y = np.random.rand(n_points)
colors = np.random.rand(n_points)
sizes = 1000 * np.random.rand(n_points)

# Create scatter plot with varying size and color
plt.scatter(x, y, c=colors, s=sizes, cmap='rainbow', alpha=0.6)
plt.colorbar(label='Color Value')
plt.title('Scatter Plot with Variable Size and Color')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.show()

Key Parameters

Parameter Description Example
c Color values for mapping c=colors
cmap Colormap to use cmap='viridis'
s Marker sizes s=sizes
alpha Transparency (0-1) alpha=0.7

Conclusion

Scatter plots with color mapping effectively visualize three-dimensional data relationships. Use different colormaps and transparency settings to enhance data interpretation and create visually appealing plots.

Updated on: 2026-03-25T17:51:25+05:30

856 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements