Strange fill between behavior
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.
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}