The `fill_between` function in Matplotlib is used for adding shaded areas between two curves. This feature is particularly useful for highlighting regions of interest, illustrating confidence intervals, or visualizing areas under curves. This tutorial will guide you through various ways to use and customize the `fill_between` function using the `matplotlib` library. ### Basic Usage of `fill_between` To get started, let's create a simple plot with a filled area between a curve and the x-axis.
import matplotlib.pyplot as plt import numpy as np # Sample data x = np.linspace(0, 10, 100) y = np.sin(x) plt.plot(x, y, label='sin(x)') plt.fill_between(x, y, color='lightblue') plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Basic fill_between') plt.legend() plt.show()
- **`fill_between(x, y, color='lightblue')`** fills the area between the curve `y = sin(x)` and the x-axis with a light blue color. ### Filling Between Two Curves You can also fill the area between two curves by providing two y-values as arguments.
import matplotlib.pyplot as plt import numpy as np # Sample data 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='lightgray') plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Filling Between Two Curves') plt.legend() plt.show()
- **`fill_between(x, y1, y2, color='lightgray')`** fills the area between the curves `y1 = sin(x)` and `y2 = cos(x)` with a light gray color. ### Adding Conditions to Fill Between You can fill areas conditionally using the `where` parameter to specify the conditions under which the area between the curves should be filled.
import matplotlib.pyplot as plt import numpy as np # Sample data x = np.linspace(0, 10, 100) y = np.sin(x) plt.plot(x, y, label='sin(x)') plt.fill_between(x, y, where=(y > 0), color='green', alpha=0.3, label='y > 0') plt.fill_between(x, y, where=(y < 0), color='red', alpha=0.3, label='y < 0') plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Conditional fill_between') plt.legend() plt.show()
- **`where=(y > 0)`** and **`where=(y < 0)`** specify the conditions under which the areas should be filled. - **`alpha=0.3`** sets the transparency level for the filled areas. ### Customizing Fill Colors and Transparency You can customize the fill colors and transparency to match your plot's aesthetics.
import matplotlib.pyplot as plt import numpy as np # Sample data 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, where=(y1 >= y2), facecolor='blue', alpha=0.3, label='sin(x) >= cos(x)') plt.fill_between(x, y1, y2, where=(y1 < y2), facecolor='orange', alpha=0.3, label='sin(x) < cos(x)') plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Custom Colors and Transparency') plt.legend() plt.show()
- **`facecolor='blue'`** and **`facecolor='orange'`** set the colors of the filled areas. - **`alpha=0.3`** adjusts the transparency level of the filled areas. ### Filling with a Pattern You can fill the area between curves using different hatch patterns.
import matplotlib.pyplot as plt import numpy as np # Sample data 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, where=(y1 >= y2), facecolor='blue', alpha=0.3, hatch='/', label='sin(x) >= cos(x)') plt.fill_between(x, y1, y2, where=(y1 < y2), facecolor='orange', alpha=0.3, hatch='\\', label='sin(x) < cos(x)') plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Filling with Patterns') plt.legend() plt.show()
- **`hatch='/'`** and **`hatch='\\'`** define different fill patterns (hatch styles) for the areas. ### Example with an Application: Confidence Interval A practical application of `fill_between` is visualizing confidence intervals:
import matplotlib.pyplot as plt import numpy as np # Sample data x = np.linspace(0, 10, 100) y = np.sin(x) y_upper = y + 0.2 y_lower = y - 0.2 plt.plot(x, y, label='Mean (sin(x))', color='blue') plt.fill_between(x, y_lower, y_upper, color='blue', alpha=0.2, label='Confidence Interval') plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Confidence Interval Visualization with fill_between') plt.legend() plt.show()
- **`fill_between(x, y_lower, y_upper, color='blue', alpha=0.2, label='Confidence Interval')`** fills the area between the lower and upper bounds, representing a confidence interval around the mean curve. ### Conclusion Using the `fill_between` function in Matplotlib allows you to add informative and visually appealing shaded regions between curves. You can customize these filled areas with various colors, patterns, and transparency settings to enhance the clarity and aesthetics of your plots.