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 plot overlapping lines in Matplotlib?
To plot overlapping lines in Matplotlib, you can control the transparency using the alpha parameter. This creates a visual overlay effect where both lines are visible even when they cross paths.
Basic Overlapping Lines
Here's how to create overlapping lines with transparency ?
import matplotlib.pyplot as plt
# Set figure size
plt.figure(figsize=(8, 5))
# Set alpha value for transparency
alpha_value = 0.7
# Plot overlapping lines
line1 = plt.plot([1, 3, 5, 2, 5, 3, 1], color='red', alpha=alpha_value, linewidth=5, label='Line 1')
line2 = plt.plot([7, 2, 5, 7, 5, 2, 7], color='green', alpha=alpha_value, linewidth=5, label='Line 2')
# Add labels and legend
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Overlapping Lines with Transparency')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
Multiple Overlapping Lines
You can create multiple overlapping lines with different alpha values ?
import matplotlib.pyplot as plt
import numpy as np
# Generate data
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.sin(x + 1)
y3 = np.sin(x + 2)
plt.figure(figsize=(10, 6))
# Plot with different alpha values
plt.plot(x, y1, color='red', alpha=0.8, linewidth=3, label='sin(x)')
plt.plot(x, y2, color='blue', alpha=0.6, linewidth=3, label='sin(x+1)')
plt.plot(x, y3, color='green', alpha=0.4, linewidth=3, label='sin(x+2)')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.title('Multiple Overlapping Sine Waves')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
Key Parameters
| Parameter | Range | Effect |
|---|---|---|
alpha |
0.0 to 1.0 | 0.0 = transparent, 1.0 = opaque |
linewidth |
> 0 | Thickness of the line |
color |
Any valid color | Line color |
Practical Example with Real Data
Here's a practical example showing temperature data over time ?
import matplotlib.pyplot as plt
import numpy as np
# Simulate temperature data
days = np.arange(1, 31)
temp_city1 = 20 + 10 * np.sin(days * 0.2) + np.random.normal(0, 2, 30)
temp_city2 = 18 + 12 * np.sin(days * 0.15) + np.random.normal(0, 1.5, 30)
plt.figure(figsize=(12, 6))
# Plot overlapping temperature lines
plt.plot(days, temp_city1, color='red', alpha=0.7, linewidth=2,
marker='o', markersize=4, label='City 1')
plt.plot(days, temp_city2, color='blue', alpha=0.7, linewidth=2,
marker='s', markersize=4, label='City 2')
plt.xlabel('Days')
plt.ylabel('Temperature (°C)')
plt.title('Temperature Comparison: Overlapping Lines')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
Conclusion
Use the alpha parameter to control transparency when plotting overlapping lines in Matplotlib. Values between 0.4-0.8 work well for most visualizations, allowing both lines to remain visible while showing their intersection points clearly.
Advertisements
