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 make Matplotlib scatterplots transparent as a group?
To make matplotlib scatterplots transparent as a group, we can adjust the alpha parameter in the scatter() method. The alpha value controls transparency, where 0 is fully transparent and 1 is fully opaque.
Basic Example with Two Groups
Let's create two groups of scatter points with different transparency levels ?
import numpy as np
import matplotlib.pyplot as plt
# Set figure size
plt.figure(figsize=(8, 6))
# Create random data for two groups
np.random.seed(42) # For reproducible results
group1_x = np.random.normal(2, 0.8, 100)
group1_y = np.random.normal(3, 0.8, 100)
group2_x = np.random.normal(4, 0.8, 100)
group2_y = np.random.normal(2, 0.8, 100)
# Plot both groups with transparency
plt.scatter(group1_x, group1_y, color='blue', alpha=0.5, s=100, label='Group 1')
plt.scatter(group2_x, group2_y, color='red', alpha=0.5, s=100, label='Group 2')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.title('Transparent Scatter Plot Groups')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
Different Alpha Values for Multiple Groups
You can apply different transparency levels to distinguish between groups ?
import numpy as np
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 6))
# Create three groups with different characteristics
np.random.seed(42)
groups_data = [
{'x': np.random.normal(1, 0.5, 80), 'y': np.random.normal(2, 0.5, 80), 'color': 'red', 'alpha': 0.3},
{'x': np.random.normal(3, 0.7, 80), 'y': np.random.normal(3, 0.7, 80), 'color': 'green', 'alpha': 0.6},
{'x': np.random.normal(5, 0.6, 80), 'y': np.random.normal(1, 0.6, 80), 'color': 'blue', 'alpha': 0.8}
]
# Plot each group with different transparency
for i, group in enumerate(groups_data, 1):
plt.scatter(group['x'], group['y'],
color=group['color'],
alpha=group['alpha'],
s=80,
label=f'Group {i} (?={group["alpha"]})')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.title('Multiple Groups with Different Transparency Levels')
plt.legend()
plt.grid(True, alpha=0.2)
plt.show()
Overlapping Groups with Global Alpha
When groups overlap significantly, transparency helps visualize density ?
import numpy as np
import matplotlib.pyplot as plt
plt.figure(figsize=(8, 6))
# Create overlapping groups
np.random.seed(42)
center_x, center_y = 3, 3
group1_x = np.random.normal(center_x - 0.5, 1, 200)
group1_y = np.random.normal(center_y, 1, 200)
group2_x = np.random.normal(center_x + 0.5, 1, 200)
group2_y = np.random.normal(center_y, 1, 200)
# Plot overlapping groups with high transparency
plt.scatter(group1_x, group1_y, color='purple', alpha=0.4, s=60, label='Group A')
plt.scatter(group2_x, group2_y, color='orange', alpha=0.4, s=60, label='Group B')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.title('Overlapping Groups with Transparency')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
Key Parameters
| Parameter | Range | Effect |
|---|---|---|
alpha |
0.0 - 1.0 | 0 = fully transparent, 1 = fully opaque |
s |
Positive number | Size of scatter points |
color |
Color name/code | Point color |
Conclusion
Use the alpha parameter in scatter() to control group transparency. Lower alpha values (0.3-0.6) work best for overlapping data, while higher values (0.7-0.9) maintain visibility for distinct groups.
Advertisements
