Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to shade points in a scatter based on colormap in Matplotlib?
To shade points in a scatter plot based on a colormap, we can use the c parameter to specify colors and cmap parameter to apply a colormap. This creates visually appealing scatter plots where point colors represent data values.
Basic Scatter Plot with Colormap
Here's how to create a scatter plot with copper colormap shading −
import numpy as np
import matplotlib.pyplot as plt
# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create random data points
x = np.random.rand(100)
y = np.random.rand(100)
# Create scatter plot with copper colormap
plt.scatter(x, y, c=x, cmap='copper')
plt.colorbar(label='Color Scale')
plt.title('Scatter Plot with Copper Colormap')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.show()
Using Different Colormaps
You can experiment with various built-in colormaps for different visual effects −
import numpy as np
import matplotlib.pyplot as plt
# Generate sample data
np.random.seed(42) # For reproducible results
x = np.random.randn(200)
y = np.random.randn(200)
colors = x + y # Color based on sum of coordinates
# Create subplots for different colormaps
fig, axes = plt.subplots(2, 2, figsize=(10, 8))
fig.suptitle('Different Colormaps for Scatter Plots')
colormaps = ['viridis', 'plasma', 'coolwarm', 'copper']
titles = ['Viridis', 'Plasma', 'Coolwarm', 'Copper']
for i, (ax, cmap, title) in enumerate(zip(axes.flat, colormaps, titles)):
scatter = ax.scatter(x, y, c=colors, cmap=cmap, alpha=0.7)
ax.set_title(title)
plt.colorbar(scatter, ax=ax)
plt.tight_layout()
plt.show()
Advanced Color Mapping
You can also use custom color values and add transparency for enhanced visualization −
import numpy as np
import matplotlib.pyplot as plt
# Create data with different patterns
np.random.seed(123)
n_points = 300
x = np.random.normal(0, 1, n_points)
y = np.random.normal(0, 1, n_points)
# Create color values based on distance from origin
colors = np.sqrt(x**2 + y**2)
# Create scatter plot with custom styling
plt.figure(figsize=(8, 6))
scatter = plt.scatter(x, y, c=colors, cmap='plasma',
alpha=0.8, s=50, edgecolors='black', linewidth=0.5)
plt.colorbar(scatter, label='Distance from Origin')
plt.title('Scatter Plot: Color by Distance from Origin')
plt.xlabel('X coordinate')
plt.ylabel('Y coordinate')
plt.grid(True, alpha=0.3)
plt.show()
Key Parameters
| Parameter | Description | Example Values |
|---|---|---|
c |
Color values for points | Array, single color, or 'red' |
cmap |
Colormap name | 'copper', 'viridis', 'plasma' |
alpha |
Transparency level | 0.0 to 1.0 |
s |
Point size | 20, 50, 100 |
Conclusion
Use the c parameter with cmap to shade scatter plot points based on data values. Popular colormaps include 'copper', 'viridis', and 'plasma' for different visual effects. Add colorbar() to show the color scale legend.
Advertisements
