Title: Matplotlib fill_between() / fill_betweenx() Function
\nURL Source: https://example.com/matplotlib/matplotlib-ref-fill_between.html
\n\n
[ Matplotlib Reference Documentation](https://example.com/matplotlib/matplotlib-apiref.html)
\nfill_between() is used to fill the area between two horizontal curves, and fill_betweenx() fills the area between two vertical curves.
Commonly used for visualizing confidence intervals, uncertainty ranges, difference areas, etc.
\nFunction Definition
\nmatplotlib.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)\nParameter Description
\n| Parameter | \nType | \nDescription | \n
|---|---|---|
| x / y | \narray | \nData points for the first coordinate axis | \n
| y1, y2 / x1, x2 | \narray or scalar | \nTwo curves. Scalar represents a constant line. y2 defaults to 0, filling to the x-axis | \n
| where | \narray of bool | \nSpecifies which intervals need to be filled. True fills, False does not fill | \n
| interpolate | \nbool | \nIf True, only interpolates exact boundaries at curve intersection points | \n
| step | \nstr | \nStep filling: 'pre', 'post', 'mid' | \n
| color / facecolor | \ncolor | \nFill color | \n
| alpha | \nfloat | \nTransparency | \n
| label | \nstr | \nLegend label | \n
| hatch | \nstr | \nFill pattern: '/', '\\', '|', '-', '+', 'x', '*' | \n
\n\nfill_between and fill_betweenx return a
\nPolyCollectionobject, which can be used for further modifications or passed to colorbar.
\n
Usage Examples
\nExample 1: Fill to x-axis (default y2=0)
\nExample
\nimport 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()\nExample 2: Confidence Interval (area between two curves)
\nExample
\nimport 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()\nExample 3: where Parameter Conditional Fill
\nExample
\nimport 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()\nExample 4: fill_betweenx Vertical Fill
\nExample
\nimport 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 +
YouTip