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 fill the area under a step curve using pyplot? (Matplotlib)
To fill the area under a step curve using pyplot, you can use the fill_between() method with the step parameter. This creates filled regions beneath step plots, which are useful for displaying discrete data or histogram-like visualizations.
Basic Step Curve with Fill
Here's how to create a simple step curve with filled area ?
import matplotlib.pyplot as plt import numpy as np # Set figure size plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Create data points x = np.linspace(-10, 10, 100) y1 = np.sin(x) y2 = np.cos(x) # Fill area under step curves plt.fill_between(x, y1, step="pre", alpha=0.4, label='sin(x)') plt.fill_between(x, y2, step="pre", alpha=0.4, label='cos(x)') # Plot step lines plt.plot(x, y1, drawstyle="steps", color='blue') plt.plot(x, y2, drawstyle="steps", color='orange') plt.legend() plt.grid(True, alpha=0.3) plt.show()
Step Parameter Options
The step parameter in fill_between() controls how the fill connects points ?
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 4, 1, 5, 3])
fig, axes = plt.subplots(1, 3, figsize=(12, 4))
# step="pre" - step occurs before the point
axes[0].fill_between(x, y, step="pre", alpha=0.5)
axes[0].plot(x, y, 'o-', drawstyle="steps-pre")
axes[0].set_title('step="pre"')
axes[0].grid(True, alpha=0.3)
# step="mid" - step occurs at the midpoint
axes[1].fill_between(x, y, step="mid", alpha=0.5)
axes[1].plot(x, y, 'o-', drawstyle="steps-mid")
axes[1].set_title('step="mid"')
axes[1].grid(True, alpha=0.3)
# step="post" - step occurs after the point
axes[2].fill_between(x, y, step="post", alpha=0.5)
axes[2].plot(x, y, 'o-', drawstyle="steps-post")
axes[2].set_title('step="post"')
axes[2].grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
Filling Between Two Step Curves
You can fill the area between two step curves by specifying both y1 and y2 parameters ?
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 20)
y1 = np.sin(x)
y2 = np.cos(x)
plt.figure(figsize=(10, 6))
# Fill area between two step curves
plt.fill_between(x, y1, y2, step="pre", alpha=0.3, color='green',
label='Area between sin(x) and cos(x)')
# Plot the step curves
plt.plot(x, y1, drawstyle="steps-pre", color='blue', label='sin(x)')
plt.plot(x, y2, drawstyle="steps-pre", color='red', label='cos(x)')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.title('Filled Area Between Two Step Curves')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
Key Parameters
| Parameter | Description | Values |
|---|---|---|
step |
Step behavior | "pre", "mid", "post" |
alpha |
Transparency level | 0.0 to 1.0 |
drawstyle |
Line drawing style | "steps", "steps-pre", "steps-mid", "steps-post" |
Conclusion
Use fill_between() with step="pre" to fill areas under step curves. Combine with drawstyle="steps" in plot() for complete step visualizations. The alpha parameter controls transparency for better visual overlap.
Advertisements
