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
Filling the region between a curve and X-axis in Python using Matplotlib
To fill the region between a curve and X-axis in Python using Matplotlib, we use the fill_between() method. This technique is useful for highlighting areas under curves, creating visualizations for statistical data, or emphasizing specific regions in plots.
Basic Syntax
The fill_between() method fills the area between two horizontal curves:
plt.fill_between(x, y1, y2, where=None, alpha=None, color=None)
Parameters
x ? Array of x-coordinates
y1, y2 ? Arrays defining the curves (y2 defaults to 0)
where ? Boolean condition to specify which areas to fill
alpha ? Transparency level (0-1)
color ? Fill color
Example 1: Basic Fill Between Curve and X-axis
Let's create a sine wave and fill the area between the curve and X-axis ?
import matplotlib.pyplot as plt
import numpy as np
# Set the figure size
plt.rcParams["figure.figsize"] = [8.00, 5.00]
plt.rcParams["figure.autolayout"] = True
# Create x and y data points
x = np.linspace(-2*np.pi, 2*np.pi, 100)
y = np.sin(x)
# Plot the curve
plt.plot(x, y, color='blue', linewidth=2, label='sin(x)')
# Fill the region between curve and X-axis
plt.fill_between(x, 0, y, color='lightblue', alpha=0.6)
# Add labels and grid
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Area Between Sine Curve and X-axis')
plt.grid(True, alpha=0.3)
plt.legend()
# Display the plot
plt.show()
Displays a sine wave with the area between the curve and X-axis filled in light blue.
Example 2: Conditional Filling
Fill only specific regions using the where parameter ?
import matplotlib.pyplot as plt
import numpy as np
# Create data
x = np.linspace(-3, 3, 100)
y = x**2 - 2
# Plot the curve
plt.figure(figsize=(8, 5))
plt.plot(x, y, color='red', linewidth=2, label='y = x² - 2')
# Fill only positive regions
plt.fill_between(x, 0, y, where=(y > 0), color='green', alpha=0.5, label='Positive area')
# Fill only negative regions
plt.fill_between(x, 0, y, where=(y < 0), color='red', alpha=0.5, label='Negative area')
# Add horizontal line at y=0
plt.axhline(y=0, color='black', linewidth=1)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Conditional Area Filling')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
Shows a parabola with positive areas filled in green and negative areas filled in red.
Example 3: Multiple Curves
Fill the area between two different curves ?
import matplotlib.pyplot as plt
import numpy as np
# Create data
x = np.linspace(0, 4, 100)
y1 = np.exp(-x)
y2 = np.exp(-2*x)
# Create the plot
plt.figure(figsize=(8, 5))
plt.plot(x, y1, color='blue', linewidth=2, label='e^(-x)')
plt.plot(x, y2, color='red', linewidth=2, label='e^(-2x)')
# Fill between the two curves
plt.fill_between(x, y1, y2, color='yellow', alpha=0.4, label='Area between curves')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Area Between Two Exponential Curves')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
Displays two exponential curves with the area between them filled in yellow.
Key Points
Use
alphaparameter for transparency controlThe
whereparameter allows conditional fillingDefault
y2=0fills between curve and X-axisCombine with
plot()to show both curve and filled area
Conclusion
The fill_between() method in Matplotlib provides flexible options for highlighting areas under curves. Use conditional parameters and transparency settings to create informative and visually appealing plots that emphasize important data regions.
