# Compile time optimized “area under analytical curve” plotting with pgfplots?

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;
\end{axis}
\end{tikzpicture}

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?

-
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

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".

\documentclass{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}

\begin{document}

\begin{tikzpicture}[/pgf/declare function={f=1-exp(-x*x);}]
\begin{axis}[axis x line=center]