TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

There are some question regarding how to plot an area under a curve with pgfplots. But as far as I can see, all the answers have in common that a function has to be plotted twice with different style options. Example, Pgfplots: how to fill the area under a curve with oblique lines (hatching) as a pattern?

That's also the way I do it currently. Here is an easy working example to play with:

\begin{tikzpicture}[/pgf/declare function={f=1-exp(-x*x);}]
  \begin{axis}[axis x line=center]
    \addplot [domain=-2:2, samples=500, red, fill] {f} \closedcycle;
    \addplot [domain=-5:5, samples=500] {f};

But especially if one has many plots with non-trivial functions (needing a lot of samples to look smooth!) this can lead to long compile times.

Just setting down the samples for the area filling plot often is not enough, because then some of the area will "overhang" on the wrong side of the curve due to the rough sampling of the plot.

Is it possible to achieve the same output with just plotting the function once to significantly reduce the compile time?

share|improve this question
Reducing the compiling time can be achieved by the library external. – Marco Daniel Apr 25 '13 at 8:50
@MarcoDaniel Yes, if you just want to ad hoc fix or "workaround" the compilation time. But coming from a C++ background, I'd still like to produce optimized code that does not need precompiled stuff. ;) – Foo Bar Apr 25 '13 at 10:07
C++ ;-) Object-oriented programming and LaTex --- Difficult ;-) – Marco Daniel Apr 25 '13 at 10:44
You could also compute the values outside of LaTeX, write them to a file and plot that. – Tom Bombadil Jun 3 '13 at 23:42
up vote 8 down vote accepted

Version 1.10 of pgfplots has been released just recently, and it comes with a new solution for the problem to fill the area between plots in a "compile-time-optimized way".

enter image description here




\begin{tikzpicture}[/pgf/declare function={f=1-exp(-x*x);}]
  \begin{axis}[axis x line=center]
    \addplot [name path=f,domain=-5:5, samples=500] {f};

    \path[name path=axis] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},0) -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},0);

    \addplot[gray!50] fill between[of=f and axis];

The solution relies on \usepgfplotslibrary{fillbetween}: it assigns name path=f to the function of interest. This is compile-time-optimized: it will use the low-level soft-path of this function. Then, it defines a \path (which is neither drawn nor filled) which resembles the x axis (and receives name path=axis. Finally, \addplot fill between fills the area under f and axis, relying only on compile-time-optimized representations. In fact, this fill operation simply connects f and axis (and computes the resulting bounding box). Consequently, it is supposed to be fast. And: it has more-or-less the same effect as \closedcycle (except that it fills between xmin and xmax which might be larger than the input data range).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.