# Strange fill between behavior

I have just started to use the fill between library and it is very useful but I admit it sometimes is a black box for me.

For example, if I want to fill the space enclosed by y=x^2 and y=2-x^2 (So clip the fill to be between -1 and 1) then I originally tried this. Then I just added one small change (decreased the ymin by a very little bit) and I got what I wanted. Can someone explain what is happening? The code I used for the first picture is below. To get the second picture, set ymin=-.1 instead of ymin=0. I also noticed that if you get rid of the soft clip in the fill between then you get the following picture which makes sense to me. So it must be something with the soft clip that is being messed up. \documentclass{standalone}
\usepackage{tikz,pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.3}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-2,xmax=2,
ymin=0,ymax=4,
height = 6cm,
minor tick num=1,
axis lines=center,
axis line style=<->]
\addplot[name path=F,blue,domain={-2:2}] {-x^2+2};
\addplot[name path=G,green,domain={-2:2}] {x^2};
\addplot[color=brown!50]fill between[of=F and G, soft clip={domain=-1:1}];
\end{axis}
\end{tikzpicture}
\end{document}

• I would guess that numerical inaccuracies are the cause of this somehow. – Torbjørn T. Jun 10 '17 at 19:11
• @TorbjørnT. What do you mean by numerical inaccuracies? – MathIsKey Jun 12 '17 at 12:40
• That when pgf calculates a value, it gets e.g. 0.99999 instead of 1, or -0.00001 instead of 0, or similar. I don't know for sure if this is the reason of course, it might some other problem with fillbetween. – Torbjørn T. Jun 12 '17 at 12:50

## 2 Answers

The area to be filled can be identified using soft clip={(-1,-1) rectangle (1,4)}. This identifies the clip area with a rectangle delineated by a left hand co-ordinate (-1-1) and a right hand co-ordinate (1,4). To avoid the fill being placed on top of the axes, axis on top must be added to the axis options. The tikz pattern library is used to fill the area with pattern=north east lines.

This is the result: This is the MWE:

\documentclass[tikz,border=5pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}
\pgfplotsset{compat=1.15}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
xmin=-2,xmax=2,
ymin=0,ymax=4,
height = 6cm,
minor tick num=1,
axis lines=center,
axis line style=<->,
axis on top
]
\addplot[name path=G,green,domain={-2:2}] {x^2};
\addplot[name path=F,blue,domain={-2:2}] {-x^2+2};
\addplot[pattern=north east lines] fill between [
of=F and G,
soft clip={(-1,-1) rectangle (1,4)}
];

\end{axis}
\end{tikzpicture}

\end{document}

• This only worked when I set the compat to 1.14 for me but that is an easy fix – MathIsKey Sep 5 '17 at 16:05

The pgf manual describes a solution as follows:

\documentclass{standalone}
\usepackage{tikz,pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.3}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
xmin=-2,xmax=2,
ymin=0,ymax=4,
height = 6cm,
minor tick num=1,
axis lines=center,
axis line style=<->
]

\addplot[name path=G,green,domain={-2:2}] {x^2};
\addplot[name path=F,blue,domain={-2:2}] {-x^2+2};
%  \addplot[color=blue!50]fill between[of=G and F, split, clip={domain=-1:1}];

\tikzfillbetween [of=F and G,split,every even segment/.style={white!1}] {red!50};

\end{axis}
\end{tikzpicture}

\end{document}


I guess the problem with the axis is not desired, but I'm sure the override can be solved somehow. • This is a nice alternate option that I will keep in mind – MathIsKey Sep 5 '17 at 16:06