YouTip LogoYouTip

Matplotlib Ref Fill_Between

Title: Matplotlib fill_between() / fill_betweenx() Function

\n

URL Source: https://example.com/matplotlib/matplotlib-ref-fill_between.html

\n
\n

[![Image 1: Matplotlib Reference Documentation](https://example.com/images/up.gif) Matplotlib Reference Documentation](https://example.com/matplotlib/matplotlib-apiref.html)

\n

fill_between() is used to fill the area between two horizontal curves, and fill_betweenx() fills the area between two vertical curves.

\n

Commonly used for visualizing confidence intervals, uncertainty ranges, difference areas, etc.

\n

Function Definition

\n
matplotlib.pyplot.fill_between(x, y1, y2=0, where=None, interpolate=False, step=None, **kwargs)matplotlib.pyplot.fill_betweenx(y, x1, x2=0, where=None, interpolate=False, step=None, **kwargs)
\n

Parameter Description

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ParameterTypeDescription
x / yarrayData points for the first coordinate axis
y1, y2 / x1, x2array or scalarTwo curves. Scalar represents a constant line. y2 defaults to 0, filling to the x-axis
wherearray of boolSpecifies which intervals need to be filled. True fills, False does not fill
interpolateboolIf True, only interpolates exact boundaries at curve intersection points
stepstrStep filling: 'pre', 'post', 'mid'
color / facecolorcolorFill color
alphafloatTransparency
labelstrLegend label
hatchstrFill pattern: '/', '\\', '|', '-', '+', 'x', '*'
\n
\n

fill_between and fill_betweenx return a PolyCollection object, which can be used for further modifications or passed to colorbar.

\n
\n
\n

Usage Examples

\n

Example 1: Fill to x-axis (default y2=0)

\n

Example

\n
import matplotlib.pyplot as plt\n\nimport numpy as np\n\nx = np.linspace(0,10,100)\n\n y = np.sin(x)\n\nfig, ax = plt.subplots(layout='constrained')\n\n# Plot the main curve\n\n ax.plot(x, y,'b-', linewidth=2, label='sin(x)')\n\n# Fill the area between the curve and the x-axis\n\n ax.fill_between(x, y, alpha=0.3, color='blue',\n\n label='Area under curve')\n\nax.set_title('fill_between: Area Between Curve and X-axis')\n\n ax.set_xlabel('x')\n\n ax.set_ylabel('sin(x)')\n\n ax.legend()\n\n ax.grid(True, alpha=0.3)\n\n plt.show()
\n

Example 2: Confidence Interval (area between two curves)

\n

Example

\n
import matplotlib.pyplot as plt\n\nimport numpy as np\n\nx = np.linspace(0,10,100)\n\n y_mean = np.sin(x) + 5# Mean line\n\n y_std =0.3 + 0.1 * x # Standard deviation increases with x\n\n# Upper and lower bounds\n\n y_upper = y_mean + y_std\n\n y_lower = y_mean - y_std\n\nfig, ax = plt.subplots(figsize=(8,5), layout='constrained')\n\n# Fill confidence intervals on both sides of the mean\n\n ax.fill_between(x, y_lower, y_upper,\n\n alpha=0.3, color='steelblue',\n\n label='Confidence Interval')\n\n# Plot Mean line\n\n ax.plot(x, y_mean,'steelblue', linewidth=2, label='Mean')\n\n# Plot upper and lower bounds\n\n ax.plot(x, y_upper,'steelblue', linewidth=0.5, alpha=0.5)\n\n ax.plot(x, y_lower,'steelblue', linewidth=0.5, alpha=0.5)\n\nax.set_title('Confidence Interval (fill_between)')\n\n ax.set_xlabel('x')\n\n ax.set_ylabel('y')\n\n ax.legend()\n\n ax.grid(True, alpha=0.3)\n\n plt.show()
\n

Example 3: where Parameter Conditional Fill

\n

Example

\n
import matplotlib.pyplot as plt\n\nimport numpy as np\n\nx = np.linspace(0,10,200)\n\n y1 = np.sin(x)\n\n y2 = np.cos(x)\n\nfig, ax = plt.subplots(figsize=(8,5), layout='constrained')\n\nax.plot(x, y1,'blue', label='sin(x)')\n\n ax.plot(x, y2,'red', label='cos(x)')\n\n# only at sin(x) >= cos(x) interval fill for\n\n ax.fill_between(x, y1, y2,\n\n where=(y1 >= y2),# Condition: fill when y1 is above y2\n\n color='blue', alpha=0.3,\n\n label='sin β‰₯ cos')\n\n# only at sin(x) < cos(x) interval fill for(different colors)\n\n ax.fill_between(x, y1, y2,\n\n where=(y1 < y2),\n\n color='red', alpha=0.3,\n\n label='sin < cos')\n\nax.set_title('Conditional Fill with where Parameter')\n\n ax.set_xlabel('x')\n\n ax.legend()\n\n ax.grid(True, alpha=0.3)\n\n plt.show()
\n

Example 4: fill_betweenx Vertical Fill

\n

Example

\n
import matplotlib.pyplot as plt\n\nimport numpy as np\n\n# fill_betweenx The parameter order is (y, x1, x2)\n\n y = np.linspace(0,10,100)\n\n x1 =2 +
← Matplotlib Ref ImshowMatplotlib Ref Figure Manageme β†’