Matplotlib Fill Between

Matplotlib Fill Between

Matplotlib is a popular data visualization library in Python that allows users to create various types of plots. One useful feature of Matplotlib is the ability to fill between two lines or curves on a plot. This can be especially useful when comparing two datasets or showing the uncertainty in data.

In this article, we will explore how to use the fill_between function in Matplotlib to fill the area between two lines or curves on a plot. We will walk through various examples to demonstrate how to customize the fill-plot and make your visualizations more impactful.

Basic fill_between Example

Let’s start with a simple example of using the fill_between function to fill the area between two lines on a plot.

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', alpha=0.5)

plt.legend()
plt.show()

Output:

Matplotlib Fill Between

In this example, we have two lines representing the sine and cosine functions. We use the fill_between function to fill the area between these two lines with a gray color and set the transparency level to 0.5. By doing this, we can visually see the difference between the two functions.

Filling Between a Line and the X Axis

Another common use case is filling the area between a line and the X axis. This can be useful for visualizing the area under a curve or highlighting a particular range of values.

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y = np.sin(x)

plt.plot(x, y, color='blue', label='sin(x)')
plt.fill_between(x, y, color='green', alpha=0.5)

plt.legend()
plt.show()

Output:

Matplotlib Fill Between

In this example, we only have the sine function plotted, and we fill the area between the curve and the X axis with a green color. This helps us visualize the area under the curve and the range of values the function takes.

Filling Between Horizontal Lines

You can also use the fill_between function to fill the area between two horizontal lines on a plot. This can be useful for highlighting a particular range or showing the difference between two values.

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y = np.ones_like(x) * 2

plt.plot(x, y, color='blue', label='y=2')
plt.axhline(y=0, color='red', label='y=0')
plt.fill_between(x, y, color='yellow', alpha=0.5)

plt.legend()
plt.show()

Output:

Matplotlib Fill Between

In this example, we have a horizontal line at y=2 and another one at y=0. We use the fill_between function to fill the area between these two lines with a yellow color. This helps us visually compare the two values and see the difference between them.

Filling Between Vertically Aligned Lines

You can also fill the area between two vertically aligned lines on a plot. This can be useful for highlighting a specific region of interest or showing the range of values between two points.

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y = np.linspace(0, 10, 100)

plt.plot(x, y, color='blue')
plt.axvline(x=3, color='red')
plt.axvline(x=7, color='red')
plt.fill_betweenx(y, x1=3, x2=7, color='purple', alpha=0.5)

plt.show()

Output:

Matplotlib Fill Between

In this example, we have a straight line aligned on the Y-axis and two vertical lines at x=3 and x=7. We use the fill_betweenx function to fill the area between these two vertical lines with a purple color. This helps us highlight the region of interest and visually represent the range of values within that range.

Customizing Fill Plots

You can customize the fill plots in Matplotlib by adjusting various parameters such as color, transparency, linestyle, etc. Let’s look at some examples of how to customize fill plots.

Changing Fill Color

You can change the fill color by specifying a different color in the color parameter of the fill_between function.

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='purple', alpha=0.5)

plt.legend()
plt.show()

Output:

Matplotlib Fill Between

In this example, we change the fill color to purple to highlight the area between the sine and cosine functions.

Adjusting Transparency

You can adjust the transparency level of the fill plot by changing the alpha parameter to a different value between 0 and 1.

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', alpha=0.2)

plt.legend()
plt.show()

Output:

Matplotlib Fill Between

In this example, we set the transparency level to 0.2, making the filled area more transparent.

Changing Line Style

You can change the line style of the fill plot by specifying a different linestyle in the linestyle parameter of the fill_between function.

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', linestyle='--', alpha=0.5)

plt.legend()
plt.show()

Output:

Matplotlib Fill Between

In this example, we change the line style to dashed (--) for the fill plot, making it visually distinct from the other lines on the plot.

Matplotlib Fill Between Conclusion

In this article, we have explored how to use the fill_between function in Matplotlib to fill the area between two lines or curves on a plot. We have covered various examples demonstrating different use cases and customization options for fill plots. By mastering the fill_between function, you can create visually appealing and informative plots in Matplotlib. Experiment with different parameters and settings to create the perfect fill plot for your data visualization needs.

Like(0)