Tx · LaTeX & TikZ

How to Fill the Region Between Two Lines in TikZ

Updated 13 October 2026

At a glance

  1. 1. Load the library

    For PGFPlots graphs, load the fillbetween library after loading pgfplots.

  2. 2. Name both paths

    Give each line a distinct name path so the fill command can refer to it.

  3. 3. Set the interval

    Use the same domain for both lines to define the x-range of the shaded region.

  4. 4. Fill and redraw

    Add a fill-between plot, then draw the two lines again so their edges stay clear.

To shade the region between two plotted lines, use PGFPlots’ fillbetween library. The key steps are to give each line a named path and then tell a fill plot which two paths to fill between.

This method is for graphs created inside a PGFPlots axis environment. If you mean arbitrary TikZ paths rather than plotted functions, see the alternative near the end.

Use PGFPlots’ fillbetween library

Load pgfplots and its fillbetween library in the preamble:

\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}

Each curve to be used in the fill needs a unique name path. Then add a plot with the syntax fill between[of=first path and second path]. Give both curves the same domain when you want to shade between them over a particular interval.

Complete example

This example shades the area between y=2x+1y=2x+1 and y=5y=5 for 0≤x≤20 \leq x \leq 2. The lines meet at the right endpoint.

\documentclass[tikz,border=4pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.18}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xmin=0, xmax=2,
    ymin=0, ymax=6,
    axis lines=middle,
    xlabel={$x$},
    ylabel={$y$},
    samples=2,
]
    % Define the two paths without drawing them yet.
    \addplot[name path=sloping, domain=0:2, draw=none] {2*x+1};
    \addplot[name path=horizontal, domain=0:2, draw=none] {5};

    % Shade the region between the named paths.
    \addplot[fill=orange!25, draw=none]
        fill between[of=sloping and horizontal];

    % Draw the lines over the fill so their edges are crisp.
    \addplot[blue, thick, domain=0:2] {2*x+1};
    \addplot[red, thick, domain=0:2] {5};
\end{axis}
\end{tikzpicture}
\end{document}

The standalone document class makes a tightly cropped figure; the example can also be placed inside a regular LaTeX document. The pgfplots package provides the axis environment, and \usepgfplotslibrary{fillbetween} enables the fill command. The name path options store the plotted curves for the fill operation. The lines are initially defined with draw=none, then drawn after the fill so they appear clearly on top of the colored region.

The shaded area in this example is a triangle. Its vertical height at x=0x=0 is 5−1=45-1=4, and its horizontal base runs from 00 to 22, so its area is 12(2)(4)=4\frac{1}{2}(2)(4)=4. Equivalently, integrating the difference between the upper and lower lines gives

∫02(5−(2x+1)) dx=∫02(4−2x) dx=4.\int_0^2 \bigl(5-(2x+1)\bigr)\,dx = \int_0^2 (4-2x)\,dx = 4.

Choose the region with the domain

The domain on each \addplot controls which part of that curve is available to the fill. If you want to shade only from x=ax=a to x=bx=b, use domain=a:b for both paths. The two paths should cover the same horizontal interval for the straightforward graph-between-functions case.

If the lines intersect within the interval, decide which pieces you want shaded. For example, if two curves cross at x=cx=c, split the interval there and create separate fill plots on either side. That avoids ambiguity about which curve is uppermost and makes it easier to style each region differently. A fill-between command does not determine the intended mathematical region from context; its input is the two paths and their shared extent.

Common mistakes

  • Forgetting the library: Without \usepgfplotslibrary{fillbetween}, PGFPlots will not recognize the fill-between operation.
  • Omitting path names: The names in of=... and ... must exactly match the name path=... values, including spelling and capitalization.
  • Using different domains: Mismatched curve extents can leave part of the fill missing or produce an unintended boundary. Set both domains explicitly.
  • Drawing an opaque fill over the lines: A later fill plot can cover earlier strokes. Use a light fill, set an opacity if desired, or redraw the curves after the fill as in the example.
  • Expecting ordinary TikZ paths to work unchanged: The fillbetween library is designed for PGFPlots paths. For simple straight segments in plain TikZ, you can instead specify the enclosed polygon directly with \fill and its corner coordinates.

If your lines are plain TikZ segments

When the endpoints are known and the boundary is a simple polygon, there is no need for PGFPlots. For example, \fill[orange!25] (0,1) -- (0,5) -- (2,5) -- cycle; fills the triangular region from the worked example. Use \draw commands afterward to add the boundary lines. For function plots or more complicated curves, named paths with PGFPlots’ fillbetween library are usually more convenient.

References

  1. PGFPlots Manual

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.