How to fill between multiple lines in Matplotlib?
×


How to fill between multiple lines in Matplotlib?

888

Introduction

Visualizing the area between multiple curves can be instrumental in understanding the relationships and differences between datasets. In Matplotlib, the fill_between() function allows you to shade the region between two curves, enhancing the interpretability of your plots. This technique is particularly useful in scenarios like displaying confidence intervals, highlighting trends, or comparing multiple datasets.

Basic Usage of fill_between()

The fill_between() function in Matplotlib fills the area between two horizontal curves. The syntax is:

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

- x: Array of x-values. - y1: Array of y-values for the first curve. - y2: Array of y-values for the second curve (default is 0). - where: Boolean array specifying where to fill. - step: Defines step function behavior. - interpolate: If True, interpolates between y1 and y2. - **kwargs: Additional parameters like color, alpha (transparency), etc.

Here's an example that demonstrates filling between two curves:

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, label='sin(x)')
plt.plot(x, y2, label='cos(x)')
plt.fill_between(x, y1, y2, color='gray', alpha=0.5)
plt.legend()
plt.show()

In this plot, the area between the sine and cosine curves is shaded with a gray color at 50% transparency.

Advanced Usage: Filling Between Multiple Curves

To fill between more than two curves, you can call fill_between() multiple times. For instance, to fill between three curves:

y3 = np.tan(x)

plt.plot(x, y1, label='sin(x)')
plt.plot(x, y2, label='cos(x)')
plt.plot(x, y3, label='tan(x)')
plt.fill_between(x, y1, y2, color='blue', alpha=0.3)
plt.fill_between(x, y2, y3, color='red', alpha=0.3)
plt.legend()
plt.show()

This approach allows you to visualize the areas between each pair of curves, providing a comprehensive view of their relationships.

Handling Intersections Between Curves

When curves intersect, it's essential to manage the filled areas appropriately. The interpolate parameter can be set to True to ensure that the fill area is correctly adjusted at the intersection points:

plt.fill_between(x, y1, y2, color='green', alpha=0.3, interpolate=True)

This ensures that the shaded area accurately reflects the region between the curves, even at their intersections.

Visualizing Multiple Filled Areas

To visualize multiple filled areas in a single plot, you can use subplots:

fig, axs = plt.subplots(3, 1, figsize=(6, 6), sharex=True)

axs[0].fill_between(x, y1, y2, color='blue', alpha=0.3)
axs[0].set_title('Fill between y1 and y2')

axs[1].fill_between(x, y2, y3, color='red', alpha=0.3)
axs[1].set_title('Fill between y2 and y3')

axs[2].fill_between(x, y1, y3, color='green', alpha=0.3)
axs[2].set_title('Fill between y1 and y3')

plt.tight_layout()
plt.show()

This method provides a clear comparison of the areas between different pairs of curves.

Conclusion

Filling the area between multiple lines in Matplotlib is a powerful technique for enhancing data visualization. By effectively using the fill_between() function, you can highlight relationships, trends, and differences between datasets, making your plots more informative and visually appealing.


If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!



Best WordPress Hosting


Share:


Discount Coupons

Unlimited Video Generation

Best Platform to generate videos

Search and buy from Namecheap

Secure Domain for a Minimum Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat