When it comes to data visualization in Python, Matplotlib remains one of my go-to libraries. Over my 10+ years of experience, I’ve found that the fill_between function is incredibly useful for highlighting areas between curves or under a line, making your charts more informative and visually appealing.
In this article, I’ll share my firsthand experience with fill_between and walk you through different ways to use it effectively.
Let’s get started.
What is Matplotlib fill_between?
fill_between is a Matplotlib function that fills the area between two curves or between a curve and a baseline. This is especially helpful when you want to emphasize ranges, confidence intervals, or differences between datasets.
Imagine you’re analyzing the average daily temperatures in New York City over a month and want to highlight the temperature range between the daily minimum and maximum. fill_between makes this easy and visually clear.
How to Use fill_between: Basic Example
Let me start with a simple example. Suppose you have daily temperatures for July in NYC, the minimum and maximum values, and you want to shade the area between them.
import matplotlib.pyplot as plt
import numpy as np
days = np.arange(1, 32)
min_temps = np.random.normal(70, 5, size=31)
max_temps = min_temps + np.random.normal(10, 3, size=31)
plt.plot(days, min_temps, label='Min Temp')
plt.plot(days, max_temps, label='Max Temp')
plt.fill_between(days, min_temps, max_temps, color='skyblue', alpha=0.5)
plt.title('July Temperature Range in NYC')
plt.xlabel('Day of July')
plt.ylabel('Temperature (°F)')
plt.legend()
plt.show()I executed the above example code and added the screenshot below.

In this snippet, fill_between shades the area between min_temps and max_temps. The alpha parameter controls transparency, making the shaded area subtle but noticeable.
Fill Between a Curve and a Baseline
Sometimes, you might only want to fill between a curve and the x-axis or another baseline. This is common when showing cumulative data or highlighting values above or below zero.
Here’s how I do it:
import matplotlib.pyplot as plt
import numpy as np
days = np.arange(1, 32)
rainfall = np.random.normal(0.2, 0.1, size=31).cumsum()
plt.plot(days, rainfall, label='Cumulative Rainfall (inches)')
plt.fill_between(days, rainfall, 0, where=(rainfall > 0), color='blue', alpha=0.3)
plt.title('Cumulative Rainfall in Seattle - July')
plt.xlabel('Day of July')
plt.ylabel('Rainfall (inches)')
plt.legend()
plt.show()I executed the above example code and added the screenshot below.

The where parameter lets you specify conditions for filling. In this case, it fills only where rainfall is above zero.
Read Module ‘matplotlib’ has no attribute ‘plot’
Highlight Specific Regions with fill_between
Another technique I often use is highlighting specific regions on a plot to mark events or periods of interest.
For example, if you want to highlight the weekend days in your data:
import matplotlib.pyplot as plt
import numpy as np
days = np.arange(1, 32)
sales = np.random.randint(100, 200, size=31)
plt.plot(days, sales, label='Daily Sales')
# Highlight weekends (assuming days 6-7, 13-14, 20-21, 27-28 as weekends)
weekends = [(6,7), (13,14), (20,21), (27,28)]
for start, end in weekends:
plt.fill_betweenx([min(sales), max(sales)], start, end, color='lightgrey', alpha=0.4)
plt.title('Daily Sales with Weekend Highlight - USA Retail Store')
plt.xlabel('Day of July')
plt.ylabel('Sales ($)')
plt.legend()
plt.show()I executed the above example code and added the screenshot below.

This fills vertical bands over weekend days, helping viewers quickly identify sales trends during weekends.
Use fill_between with Multiple Curves
When comparing multiple datasets, fill_between can emphasize the difference between them.
Say you’re comparing two competing products’ daily sales:
import matplotlib.pyplot as plt
import numpy as np
days = np.arange(1, 32)
product_a = np.random.randint(100, 200, size=31)
product_b = np.random.randint(80, 220, size=31)
plt.plot(days, product_a, label='Product A')
plt.plot(days, product_b, label='Product B')
plt.fill_between(days, product_a, product_b, where=(product_a > product_b), color='green', alpha=0.3, label='A > B')
plt.fill_between(days, product_a, product_b, where=(product_a <= product_b), color='red', alpha=0.3, label='B ≥ A')
plt.title('Comparing Daily Sales: Product A vs Product B')
plt.xlabel('Day of July')
plt.ylabel('Sales ($)')
plt.legend()
plt.show()This approach visually separates when Product A outsells Product B and vice versa, using green and red shading.
Customize fill_between: Colors, Transparency, and Styles
From my experience, the visual impact of fill_between depends heavily on color choices and transparency.
- Use contrasting colors to differentiate filled areas from lines.
- Adjust
alphafor subtlety, values between 0.2 and 0.5 usually work well. - You can also use hatch patterns for print-friendly visuals by setting the
hatchparameter.
Example:
plt.fill_between(days, min_temps, max_temps, color='orange', alpha=0.4, hatch='//')Practical Tips for Using fill_between
- Data alignment: Ensure your x-values and y-values arrays are aligned and of the same length to avoid errors.
- Handling NaNs:
fill_betweenskips NaNs, which can break fills. Clean your data or interpolate missing values. - Performance: For large datasets, consider downsampling before plotting to keep your plots responsive.
Check out Matplotlib Scatter Plot Color
Conclusion
fill_between is a useful yet easy function that can elevate your Matplotlib plots by adding meaningful shaded areas. Whether you’re visualizing weather data, sales trends, or survey results from across the USA, mastering fill_between helps you communicate insights.
Try experimenting with different parameters and use cases. The more you use it, the more you’ll find creative ways to enhance your visualizations.
You may also read other Matplotlib-related articles:
- Matplotlib Pie Chart Tutorial
- Matplotlib Update Plot in Loop
- Set Loglog Log Scale for X and Y Axes in Matplotlib

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.