Skip to main content
ubuntuask.com

Back to all posts

How to Fill Between Multiple Lines In Matplotlib?

Published on
4 min read
How to Fill Between Multiple Lines In Matplotlib? image

Best Matplotlib Learning Resources to Buy in October 2025

1 Effective Visualization: Exploiting Matplotlib & Pandas (Treading on Python)

Effective Visualization: Exploiting Matplotlib & Pandas (Treading on Python)

BUY & SAVE
$49.00
Effective Visualization: Exploiting Matplotlib & Pandas (Treading on Python)
2 Data Visualization in Python with Matplotlib: The Complete Guide to Mastering Python

Data Visualization in Python with Matplotlib: The Complete Guide to Mastering Python

BUY & SAVE
$9.99
Data Visualization in Python with Matplotlib: The Complete Guide to Mastering Python
3 Mastering Data Analysis with Python: A Comprehensive Guide to NumPy, Pandas, and Matplotlib

Mastering Data Analysis with Python: A Comprehensive Guide to NumPy, Pandas, and Matplotlib

BUY & SAVE
$19.99
Mastering Data Analysis with Python: A Comprehensive Guide to NumPy, Pandas, and Matplotlib
4 Python Cheat Sheet, Guide by Examples, Cover all Basic Python Syntaxes, Complete Reference (2025.01): Python Programming Syntax Table & Chart, Quick Study Workbook, Syntax Dictionary

Python Cheat Sheet, Guide by Examples, Cover all Basic Python Syntaxes, Complete Reference (2025.01): Python Programming Syntax Table & Chart, Quick Study Workbook, Syntax Dictionary

BUY & SAVE
$19.99
Python Cheat Sheet, Guide by Examples, Cover all Basic Python Syntaxes, Complete Reference (2025.01): Python Programming Syntax Table & Chart, Quick Study Workbook, Syntax Dictionary
5 Data Analysis Foundations with Python: Master Python and Data Analysis using NumPy, Pandas, Matplotlib, and Seaborn: A Hands-On Guide with Projects ... From Basics to Real-World Applications)

Data Analysis Foundations with Python: Master Python and Data Analysis using NumPy, Pandas, Matplotlib, and Seaborn: A Hands-On Guide with Projects ... From Basics to Real-World Applications)

BUY & SAVE
$39.90
Data Analysis Foundations with Python: Master Python and Data Analysis using NumPy, Pandas, Matplotlib, and Seaborn: A Hands-On Guide with Projects ... From Basics to Real-World Applications)
6 Python Data Analytics and Visualization for Beginners: A Hands-On Guide to Exploring and Visualizing Data with Pandas and Matplotlib (Python MEGA bundle)

Python Data Analytics and Visualization for Beginners: A Hands-On Guide to Exploring and Visualizing Data with Pandas and Matplotlib (Python MEGA bundle)

BUY & SAVE
$24.99
Python Data Analytics and Visualization for Beginners: A Hands-On Guide to Exploring and Visualizing Data with Pandas and Matplotlib (Python MEGA bundle)
7 Python and Matplotlib Essentials for Scientists and Engineers (Iop Concise Physics)

Python and Matplotlib Essentials for Scientists and Engineers (Iop Concise Physics)

BUY & SAVE
$23.17 $39.95
Save 42%
Python and Matplotlib Essentials for Scientists and Engineers (Iop Concise Physics)
8 Python Data Mining Quick Start Guide: A beginner's guide to extracting valuable insights from your data

Python Data Mining Quick Start Guide: A beginner's guide to extracting valuable insights from your data

BUY & SAVE
$24.85 $32.99
Save 25%
Python Data Mining Quick Start Guide: A beginner's guide to extracting valuable insights from your data
9 MATLAB Programming, For Beginners, Quick Start Guide: Matlab Language Crash Course Tutorial & Exercises (Paperbacks in 8 Hours)

MATLAB Programming, For Beginners, Quick Start Guide: Matlab Language Crash Course Tutorial & Exercises (Paperbacks in 8 Hours)

BUY & SAVE
$13.99
MATLAB Programming, For Beginners, Quick Start Guide: Matlab Language Crash Course Tutorial & Exercises (Paperbacks in 8 Hours)
+
ONE MORE?

To fill between multiple lines in matplotlib, you can use the fill_between function provided by the library. This function allows you to specify the x values and the y values for the region that you want to fill between two or more lines.

You can pass the x values as a list or array, and the y values for each line as separate lists or arrays. By providing the y values for multiple lines, you can fill the area between them. Additionally, you can specify the color and transparency of the filled region by passing the color and alpha parameters to the fill_between function.

Overall, filling between multiple lines in matplotlib is a straightforward process that allows you to visually highlight the area between the lines in your plot.

How to fill between lines with custom patterns in matplotlib?

To fill between lines with custom patterns in matplotlib, you can use the fill_between function and set the hatch parameter to the desired pattern. Here is an example code snippet to demonstrate how to fill between two lines with diagonal stripes pattern:

import matplotlib.pyplot as plt import numpy as np

x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x)

plt.plot(x, y1, color='blue', label='sin(x)') plt.plot(x, y2, color='red', label='cos(x)')

plt.fill_between(x, y1, y2, color='gray', hatch='//', alpha=0.5)

plt.legend() plt.show()

In this example, the fill_between function is used to fill the area between the sin(x) and cos(x) lines with a gray color and diagonal stripes pattern indicated by '//'. You can customize the pattern by changing the value of the hatch parameter to other predefined patterns like '/', 'x', '-', etc.

What is the purpose of fill_between in matplotlib?

The fill_between function in Matplotlib is used to fill the area between two horizontal curves. It allows you to visually represent the area between two line plots or curves in a plot. This can be useful for highlighting certain regions in a graph or showing the difference between two sets of data.

What is the syntax for fill_between in matplotlib?

The syntax for the fill_between function in Matplotlib is as follows:

fill_between(x, y1, y2=0, where=None, interpolate=False, step=None, **kwargs)

  • x: Array of x values.
  • y1: Array of y values to fill between (lower bound).
  • y2: Array of y values to fill between (upper bound). Default is 0.
  • where: Array of boolean values indicating where to fill between or not. Default is None.
  • interpolate: If True, the filled area will be interpolated smoothly. Default is False.
  • step: If True, the step style is used. Default is None.
  • **kwargs: Additional keyword arguments to customize the appearance of the filled area (e.g., color, alpha, linewidth).

How to fill between lines with horizontal stripes in matplotlib?

To fill between two lines with horizontal stripes in Matplotlib, you can use the hatch parameter of the fill_between function. Here's an example code snippet that demonstrates how to achieve this:

import numpy as np import matplotlib.pyplot as plt

Generate some example data

x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x)

Plot the two lines

plt.plot(x, y1, color='blue') plt.plot(x, y2, color='green')

Fill between the two lines with horizontal stripes

plt.fill_between(x, y1, y2, color='none', edgecolor='black', hatch='//')

plt.show()

In this code snippet, we first plot two lines using the plot function. Then, we use the fill_between function to fill the area between the two lines with horizontal stripes using the hatch parameter set to //. You can customize the color, linewidth, and spacing of the stripes by adjusting the parameters in the fill_between function.

How to fill between lines with patterned fill in matplotlib?

To fill between lines with a patterned fill in matplotlib, you can use the fill_betweenx() or fill_between() functions along with the hatch parameter. Here's an example of how to do this:

import matplotlib.pyplot as plt import numpy as np

x = np.arange(0, 10, 0.1) y1 = np.sin(x) y2 = np.cos(x)

plt.plot(x, y1, color='blue') plt.plot(x, y2, color='green')

plt.fill_between(x, y1, y2, color='gray', hatch='//', alpha=0.5)

plt.show()

In this example, we first create two lines using the plot() function. We then use the fill_between() function to fill the space between the two lines with a gray color and a diagonal line pattern specified by hatch='//'. The alpha parameter specifies the transparency level of the filled region.

You can also use the fill_betweenx() function if you want to fill between horizontal lines. Just make sure to adjust the parameters accordingly.