Filling an area between plots

With pgfplots 1.10 and its fillbetween library we can fill the area between curves in a new convenient way:

  • draw the first function and give it a name
  • draw the second function and name it too
  • add a fill between plot

This Code was posted by Stefan Kottwitz to TeX.SE, modifying an answer of Gonzalo Medina there.

Fill area

Edit and compile if you like:
\pgfplotsset{width=7cm, compat=1.10}
    axis y line = left,
    axis x line = bottom,
    xtick       = {-1.2,2,4.2},
    xticklabels = {$a$,$\zeta$,$b$},
    ytick       = {3},
    yticklabels = {$f(\zeta)$},
    samples     = 160,
    domain      = -1.2:4.2,
    xmin = -2, xmax = 5,
    ymin = -5, ymax = 10,
  \addplot[name path=poly, black, thick, mark=none, ] {poly};
  \addplot[name path=line, gray, no markers, line width=1pt] {3};
  \addplot fill between[ 
    of = poly and line, 
    split, % calculate segments
    every even segment/.style = {orange!70},
    every odd segment/.style  = {gray!60}
Click to download: fill-between-plots.texfill-between-plots.pdf