Related Articles
Matplotlib.pyplot.fill_between() in Python
• Last Updated : 19 Apr, 2020

Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack.

## matplotlib.pyplot.fill_between()

The matplotlib.pyplot.fill_between() is used to fill area between two horizontal curves. Two points (x, y1) and (x, y2) define the curves. this creates one or more polygons describing the filled areas. The ‘where’ parameter can be used to selectively fill some areas. By default, edges connect the given points directly. The ‘step’ parameter is used if the filling needs to be a step function.

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

Parameters:

1. x: It is array of length N. These are the y coordinates of the nodes that define the curves.
2. y1:It is an array of length N or a scalar. This represents the x coordinates of the nodes that define the first curve.
3. y2: It is an array of length N and is optional in nature. Its default value is 0. This represents the x coordinates of the nodes that define the second curve.
4. where: it is an array of boolean values of length N. It is defined if there is a need to exclude some vertical regions from being filled. It is important to note that this definition means that an isolated true value in between two two false values is where it will not do the filling. Adjacent False values results in not filling both sides of the True value.
5. interpolate: It is an optional parameter that accepts boolean values. It is only relevant if where is used and two curves are crossing each other. Semantically where if generally used for y1>y2 or similar cases. By default the filled regions will be placed at the x-array positions defining a filled polygonal area. The section of x that has the intersection are simply clipped. Setting this parameter to True results in calculation of the actual point of intersection and extends to the filled regions till the points.
6. step: This is an optional parameter that accepts one of the three values namely, ‘pre’, ‘post’ and ‘mid’. This is used to specify where the steps will occur.
• pre: From every y position the x value is continued constantlyto the left, ie, the interval (x[i-1], x[i]) has the value y[i].
• post:From every y position the x value is continued constantly to the right, ie, the interval (x[i], x[i+1]) has the value y[i].
• mid: Half way between the x positions these steps occur.

Returns: It returns a plotted polygon from the PolyCollection.

other Parameters: **kwargs contains keywords from PolyCollection that controls the polygon properties;

PropertyDescription
agg_filtera filter function that takes a (m, n, 3) float array and a dpi value that returns a (m, n, 3) array
alphafloat or None
animatedbool
arrayndarray
capstyle{‘butt’, ’round’, ‘projecting’}clima length 2 sequence of floats; may be overridden in methods that have vmin and vmax kwargs.
cmapcolormap or registered colormap
antialiased or aa or antialiasedsbool or sequence of bools
clip_boxBbox
clip_onbool
clip_path[(Path, Transform)|Patch|None]
colorcolor or sequence of rgba tuples
containscallable
edgecolor or ec or edgecolorscolor or sequence of colors or ‘face’
facecolor or fc or facecolorscolor or sequence of colors
figurefigure
gidstr
hatch{‘/’, ‘\’, ‘|’, ‘-‘, ‘+’, ‘x’, ‘o’, ‘O’, ‘.’, ‘*’}
in_layoutbool
joinstyle{‘miter’, ’round’, ‘bevel’}
linestyle or ls or linestyles or dashes{‘-‘, ‘–‘, ‘-.’, ‘:’, ”, (offset, on-off-seq), …}
linewidth or linewidths or lwfloat or sequence of floats
normNormalize
offset_position{‘screen’, ‘data’}
offsetsfloat or sequence of floats
path_effectsAbstractPathEffect
pickerNone or bool or float or callable
path_effectsAbstractPathEffect
pickerfloat or callable[[Artist, Event], Tuple[bool, dict]]
rasterizedbool or None
sketch_params(scale: float, length: float, randomness: float)
snapbool or None
transformmatplotlib.transforms.Transform
urlstr
urlsList[str] or None
visiblebool
xdata1D array
zorderfloat

Example 1:

 `import` `matplotlib.pyplot as plt``import` `numpy as np`` ` `x ``=` `np.arange(``0``,``10``,``0.1``)`` ` `# plotting the lines``a1 ``=` `4` `-` `2``*``x``a2 ``=` `3` `-` `0.5``*``x``a3 ``=` `1` `-``x`` ` `# The upper edge of``# polygon``a4 ``=` `np.minimum(a1, a2)`` ` `# Setting the y-limit``plt.ylim(``0``, ``5``)`` ` `# Plot the lines``plt.plot(x, a1,``        ``x, a2,``        ``x, a3)`` ` `# Filling between line a3 ``# and line a4``plt.fill_between(x, a3, a4, color``=``'green'``,``                 ``alpha``=``0.5``)``plt.show()`

Output: Example 2:

 `import` `matplotlib.pyplot as plt``import` `numpy as np`` ` ` ` `a ``=` `np.linspace(``0``,``2``*``3.14``,``50``)``b ``=` `np.sin(a)`` ` `plt.fill_between(a, b, ``0``,``                 ``where ``=` `(a > ``2``) & (a <``=` `3``),``                 ``color ``=` `'g'``)``plt.plot(a,b)`

Output: Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up