3

Minimal example:

\documentclass{minimal}
\usepackage{tikz}
\usepackage{pgfplots}
%\pgfplotsset{compat=newest} % has no effect here

\begin{document}

\begin{tikzpicture}
\begin{axis}[]
% Does not work with table or coordinates
\def \pts {(0.1,0.075) (0.15,0.45) (0.20,0.475) (0.25,0.175) (0.3,0.025)}
\addplot[smooth] coordinates {\pts};
\addplot[smooth, fill=red, domain={0.2:0.25}] coordinates {\pts} \closedcycle;
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[]
% Does not work with table or coordinates
\def \pts {(0.1,0.075) (0.15,0.45) (0.20,0.475) (0.25,0.175) (0.3,0.025)}
\addplot[smooth] coordinates {\pts};
\addplot[smooth, fill=red, restrict x to domain={0.2:0.25}] coordinates {\pts} \closedcycle;
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[]
% Works with expression
\addplot[smooth, domain=0:20] {x*x};
\addplot[smooth, fill=blue, domain={10.5:12}] {x*x} \closedcycle;
\end{axis}
\end{tikzpicture}

\end{document}

Result: Minimal example result

The problem with the second graph is that the plot is jagged (because the smoothing/interpolation algorithm does not have the same inputs).

  • That's why you should never smooth your plots. restrict <x,y> to domain discards the coordinates. – percusse Apr 05 '15 at 13:53
  • I know, but I do not have these points in between. So is there now way to fill an area (with restricted domain) under the plot? – user75636 Apr 05 '15 at 14:12
  • 1
    For now I will use the solution from here http://tex.stackexchange.com/questions/164991/pgfplots-how-to-fill-bounded-area-under-a-curve-using-addplot-and-fill as a workaround. – user75636 Apr 05 '15 at 14:22
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. –  Apr 05 '15 at 14:43

1 Answers1

2

As already pointed out in comments, smooth and domain restrictions do not work well together in order to fill the area between a curve and the axis.

However, you can use the fillbetween library which allows to fill paths between any two plots. In order to use it, we need to add the X axis as an invisible (but named) dummy plot, and use soft clip in order to restrict the domain of the filled area:

\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.12}

\begin{document}

\begin{tikzpicture}
\begin{axis}[]
\def \pts {(0.1,0.075) (0.15,0.45) (0.20,0.475) (0.25,0.175) (0.3,0.025)}
\addplot[name path=PTS,smooth] coordinates {\pts};
\path[name path=AXIS] (0.1,0) -- (0.3,0);

\addplot[fill=red] fill between[of=PTS and AXIS,soft clip={domain={0.2:0.25}}];
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[]
\def \pts {(0.1,0.075) (0.15,0.45) (0.20,0.475) (0.25,0.175) (0.3,0.025)}
\addplot[name path=PTS,smooth] coordinates {\pts};
\path[name path=AXIS] (0.1,0) -- (0.3,0);
\addplot[fill=red] fill between[of=PTS and AXIS,soft clip={domain={0.2:0.25}}];
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[]
\addplot[name path=PTS,smooth, domain=0:20] {x*x};
\path[name path=AXIS] (0,0) -- (20,0);
\addplot[fill=blue] fill between[of=PTS and AXIS,soft clip={domain={10.5:12}}];
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here