I’ve found that filling areas between lines is one of the most visually effective ways to highlight ranges and intervals in your plots. Whether you want to emphasize a confidence interval, mark a threshold, or simply make your charts more intuitive, using Matplotlib’s fill capabilities can make a big difference.
In this tutorial, I will walk you through how to fill between two horizontal lines and two vertical lines in Matplotlib. I’ll share multiple methods for each, complete with full Python code examples you can use right away. This guide is perfect for anyone looking to enhance their Python visualization skills with practical, real-world examples.
How to Fill Between Two Horizontal Lines in Matplotlib
Filling between two horizontal lines is a common requirement when you want to highlight a specific y-range on your plot. I’ll show you two easy methods to achieve this.
Method 1: Use axhspan() to Fill Between Horizontal Lines
The easiest way to fill between two horizontal lines in Matplotlib is by using the axhspan() function. This function fills a horizontal span across the axes, between two y-values.
Here’s a practical example where I highlight a temperature range between 60°F and 75°F on a daily temperature chart for New York City:
import matplotlib.pyplot as plt
# Sample data: Daily temperatures in NYC (°F)
days = range(1, 11)
temps = [55, 62, 70, 68, 72, 74, 69, 65, 60, 58]
fig, ax = plt.subplots()
# Plot the temperature line
ax.plot(days, temps, label='Daily Temperature', color='blue')
# Fill between two horizontal lines (60°F to 75°F)
ax.axhspan(60, 75, color='orange', alpha=0.3, label='Comfortable Range')
# Adding labels and legend
ax.set_xlabel('Day')
ax.set_ylabel('Temperature (°F)')
ax.set_title('NYC Daily Temperatures with Comfortable Range Highlighted')
ax.legend()
plt.show()You can see the output in the screenshot below.

This method is simple and effective. The axhspan() fills the entire horizontal width of the plot between the two y-values you specify. Adjust the transparency with the alpha parameter to keep the plot readable.
Method 2: Use fill_between() with Custom X Limits
If you want more control over the horizontal extent of the fill (i.e., not spanning the entire x-axis), you can use fill_between() with horizontal lines by specifying the y-values and a range of x-values.
Here’s how I used it to highlight a temperature comfort zone only during weekdays (days 1 to 5):
import matplotlib.pyplot as plt
import numpy as np
days = np.arange(1, 11)
temps = [55, 62, 70, 68, 72, 74, 69, 65, 60, 58]
fig, ax = plt.subplots()
ax.plot(days, temps, label='Daily Temperature', color='blue')
# Define x range for weekdays
x_fill = np.arange(1, 6)
# Fill between two horizontal lines (60°F to 75°F) only for weekdays
ax.fill_between(x_fill, 60, 75, color='green', alpha=0.3, label='Weekday Comfort Zone')
ax.set_xlabel('Day')
ax.set_ylabel('Temperature (°F)')
ax.set_title('NYC Daily Temperatures with Weekday Comfort Zone')
ax.legend()
plt.show()You can see the output in the screenshot below.

This approach gives you flexibility to fill only specific parts of the plot horizontally, which can be useful for highlighting different time periods or segments.
How to Fill Between Two Vertical Lines in Matplotlib
Sometimes, you want to emphasize a vertical range on your plot, for example, a specific date range or a period of interest. Matplotlib offers a couple of ways to fill between two vertical lines effectively.
Method 1: Use axvspan() to Fill Between Vertical Lines
Similar to axhspan(), Matplotlib’s axvspan() fills a vertical span between two x-values across the entire y-axis.
Here’s an example where I highlight the summer months (June to August) on a year-long temperature trend for Chicago:
import matplotlib.pyplot as plt
import numpy as np
# Days of the year (1 to 365)
days = np.arange(1, 366)
# Simulated average daily temperature for Chicago
temps = 30 + 20 * np.sin(2 * np.pi * (days - 172) / 365) # peak at day 172 (June 21)
fig, ax = plt.subplots()
ax.plot(days, temps, label='Avg Daily Temp', color='red')
# Highlight summer (day 152 = June 1, day 243 = August 31)
ax.axvspan(152, 243, color='yellow', alpha=0.3, label='Summer Period')
ax.set_xlabel('Day of Year')
ax.set_ylabel('Temperature (°F)')
ax.set_title('Chicago Average Daily Temperature with Summer Highlight')
ax.legend()
plt.show()You can see the output in the screenshot below.

This method is easy and works well when you want to highlight an entire vertical range across the plot.
Method 2: Use fill_between() with Custom Y Limits
If you want to fill a vertical band but only over a specific y-range (not the entire height), you can use fill_between() by swapping the roles of x and y.
For example, to highlight a period where temperatures are between 40°F and 60°F during the spring months (days 80 to 171):
import matplotlib.pyplot as plt
import numpy as np
days = np.arange(1, 366)
temps = 30 + 20 * np.sin(2 * np.pi * (days - 172) / 365)
fig, ax = plt.subplots()
ax.plot(days, temps, label='Avg Daily Temp', color='red')
# Define the vertical range (days 80 to 171)
x_fill = np.arange(80, 172)
# Fill between vertical lines only between 40°F and 60°F
ax.fill_between(x_fill, 40, 60, color='cyan', alpha=0.3, label='Spring Moderate Temp Range')
ax.set_xlabel('Day of Year')
ax.set_ylabel('Temperature (°F)')
ax.set_title('Chicago Temperature with Spring Moderate Range Highlight')
ax.legend()
plt.show()You can see the output in the screenshot below.

This method provides precise control over both the horizontal and vertical extents of your fill, making it ideal for more complex highlighting.
Using these methods, you can easily enhance your Python Matplotlib visualizations by filling between horizontal and vertical lines. Whether you want to emphasize temperature comfort zones, seasonal periods, or any range in your data, these techniques will help you clearly communicate insights.
Related Python Guides you may also like:
- How to Use tight_layout and bbox_inches in Matplotlib
- Matplotlib Scatter Plots with Tight_Layout in Python
- Matplotlib fill_between Hatch Color and Facecolor
- How to Use Matplotlib fill_between with Edge and No Edge

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.