In Matplotlib, plot fill related functions provide the ability to fill specific areas of a graph with color. This is useful for highlighting ranges of data, or to visually represent certain conditions.
The main functions are fill_between(), fill_betweenx(), and fill().
1. fill_between()
fill_between() fills the area between two curves or lines. It is primarily used to fill a range of y values based on x values.
plt.fill_between(x, y1, y2=0, where=None, interpolate=False, alpha=0.5, color='blue')
- x: X-axis data.
- y1: First y-axis data.
- y2: Second y-axis data (defaults to 0).
- where: Specify a condition to fill only certain areas.
- interpolate: If true, interpolate where conditions are not met.
- alpha: Transparency (0 to 1).
- color: Fill color.
2. Conditional fill (where parameter)
Use the where parameter to populate only areas that meet certain conditions.
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y, label='sin(x)')
plt.fill_between(x, y, where=(y > 0), color='green', alpha=0.3, label='y > 0')
plt.fill_between(x, y, where=(y < 0), color='red', alpha=0.3, label='y < 0')
plt.title('Conditional Fill Between')
plt.legend()
plt.show()
3. Fill between two curves
You can also fill the area between two curves.
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
plt.plot(x, y1, label='sin(x)')
plt.plot(x, y2, label='cos(x)')
plt.fill_between(x, y1, y2, color='purple', alpha=0.3)
plt.title('Fill Between Two Curves')
plt.legend()
plt.show()
4. fill_betweenx()
fill_betweenx() is similar to fill_between(), but fills a range of x values based on y values.
plt.fill_betweenx(y, x1, x2=0, where=None, interpolate=False, alpha=0.5, color='blue')
5. fill()
fill() is used to fill a polygonal area. Define the polygon by specifying the x and y coordinates.
plt.fill(x, y, color='blue', alpha=0.5)
6. Filling multiple areas
You can also fill multiple areas with different colors.
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
plt.plot(x, y1, label='sin(x)')
plt.plot(x, y2, label='cos(x)')
plt.fill_between(x, y1, y2, where=(y1 > y2), color='green', alpha=0.3, label='sin(x) > cos(x)')
plt.fill_between(x, y1, y2, where=(y1 < y2), color='red', alpha=0.3, label='sin(x) < cos(x)')
plt.title('Multiple Filled Areas')
plt.legend()
plt
Plot fill Code
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 100)
y1 = np.sin(x)
y2 = np.cos(x)
plt.fill_between(x, y1, y2, color='orange', alpha=0.5)
plt.plot(x, y1, color='blue', label='sin(x)')
plt.plot(x, y2, color='green', label='cos(x)')
plt.title('Fill Between Example')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.legend()
plt.show()

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 100)
y1 = np.sin(x)
plt.fill_between(x, y1, color='purple', alpha=0.5, where=(y1 > 0), label='y1 > 0')
plt.fill_between(x, y1, color='red', alpha=0.5, where=(y1 <= 0), label='y1 <= 0')
plt.plot(x, y1, color='black')
plt.title('Fill Between Axis Example')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.legend()
plt.show()

Necessity of Filled Areas in Matplotlib
1. Visual Emphasis
- Highlighting specific areas: You can emphasize certain ranges or conditions in your data. For example, filling the area between two curves makes the difference clear.
- Improving readability: Filled areas make important parts of the graph stand out, making data interpretation easier.
2. Versatility
- Conditional fill: The
where
parameter allows you to fill only the areas that meet specific conditions. - Flexible styling: You can customize colors, transparency (
alpha
), gradients, and more to create visually rich graphs.
3. Representing Data Ranges
- Visualizing ranges: Use filled areas to show minimum/maximum ranges, confidence intervals, error bars, and more.
- Facilitating comparisons: Representing differences between two datasets as a filled area makes comparisons more intuitive.
4. Flexibility
- Filling polygons: The
fill()
function allows you to fill arbitrary polygonal shapes. - Vertical/horizontal fills: Use
fill_between()
andfill_betweenx()
to fill vertical or horizontal areas efficiently.
Things to Watch Out For
1. Performance Issues
- Slow on large datasets: Filling operations can become slow when working with a high number of data points.
- Slow rendering in complex graphs: Graphs with multiple filled areas may take longer to process.
2. Poor Readability
- Clutter when overused: Too many filled areas can make the graph look messy and harder to interpret.
- Need for transparency settings: Without setting transparency (
alpha
), filled areas may obscure other elements.
3. Complexity of Settings
- Conditional fills can be tricky: Using the
where
parameter for conditional fills can lead to complex and hard-to-manage code. - Styling difficulty: Additional code is required to fine-tune colors, transparency, and borders for a polished look.
4. Limitations
- Curve intersection issues: If two curves intersect, extra processing may be needed when using
fill_between()
. - Polygon fill limitations: The
fill()
function requires exact vertex positions, making complex shapes harder to implement.
Using filled areas effectively can enhance data visualization, but careful planning is needed to avoid performance and readability issues.