Example: Filling an area between plots

Published 2014-03-15 | Authors: Stefan Kottwitz, Gonzalo Medina

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.

Download as: [PDF] [TEX]  •  [Open in Overleaf]

Filling an area between plots

Do you have a question regarding this example, TikZ or LaTeX in general? Just ask in the LaTeX Forum.
Oder frag auf Deutsch auf TeXwelt.de.

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=7cm, compat=1.10}
\usepgfplotslibrary{fillbetween}
\pgfmathdeclarefunction{poly}{0}{\pgfmathparse{-x^3+5*(x^2)-3*x-3}}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    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}
  ];
\end{axis}
\end{tikzpicture}
\end{document}

Comments

Adding comments is currently not enabled.

There are currently 0 comments on this entry.

Features
Tags
Scientific and technical areas

Creative Commons License