8

I want to fill the area between two curves. What I have so far:

\documentclass{article}
\usepackage{tikz,pgfplots}
\usetikzlibrary{patterns}
\usepgfplotslibrary{fillbetween}
\begin{document}

\begin{center}
\begin{tikzpicture}

\begin{axis}[%
width=6cm,
height=5cm,
at={(0cm,0cm)},
scale only axis,
separate axis lines,
every outer x axis line/.append style={black},
every x tick label/.append style={font=\color{black}},
xmin=0,
xmax=2.501,
every outer y axis line/.append style={black},
every y tick label/.append style={font=\color{black}},
ymin=0,
ymax=2.501,
axis background/.style={fill=white}
]

\addplot [color=red,name path=A]
table[row sep=crcr]{%
    0                   0.6\\
    1                   1\\
    2.5109328918269 1.38577280784458\\
};
\addplot [color=blue,name path=B]
table[row sep=crcr]{%
    0                   -1\\
    0.587895504187662   0\\
    1                   1\\
    1.38809536149591    2.5121495529328\\
};

\addplot[pattern=north east lines] fill between[of=A and B ];
\end{axis}
\end{tikzpicture}%
\end{center}
\end{document}

This results in:

enter image description here

However, I want to fill only the area left to the intersection. I already tried soft clip etc. but without success.

8

You can use intersection segments:

\fill[
    intersection segments={
      of=A and B,
      sequence={L1--R1[reverse]}
    },
    pattern=north east lines,
  ]-- cycle;

L1 is the first segement of the left path (in the example path A) and R1[reverse] is the reversed first segment of the right path (in the example path B).

enter image description here

Code:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{patterns}
\usepgfplotslibrary{fillbetween}
\begin{document}

\begin{center}
\begin{tikzpicture}

\begin{axis}[%
width=6cm,
height=5cm,
at={(0cm,0cm)},
scale only axis,
separate axis lines,
every outer x axis line/.append style={black},
every x tick label/.append style={font=\color{black}},
xmin=0,
xmax=2.501,
every outer y axis line/.append style={black},
every y tick label/.append style={font=\color{black}},
ymin=0,
ymax=2.501,
axis background/.style={fill=white}
]

\addplot [color=red,name path=A]
table[row sep=crcr]{%
    0                   0.6\\
    1                   1\\
    2.5109328918269 1.38577280784458\\
};
\addplot [color=blue,name path=B]
table[row sep=crcr]{%
    0                   -1\\
    0.587895504187662   0\\
    1                   1\\
    1.38809536149591    2.5121495529328\\
};

\fill[
    intersection segments={
      of=A and B,
      sequence={L1--R1[reverse]}
    },
    pattern=north east lines,
  ]-- cycle;
\end{axis}
\end{tikzpicture}%
\end{center}
\end{document}
7

fillbetween allows you to split the filling into the different segments made by intersections, and add individual styles to each, so you can do e.g.

\addplot fill between[
   of=A and B,
   split, 
   every segment no 0/.style={pattern=north east lines},
   every segment no 1/.style={fill=none}];

enter image description here

\documentclass{article}
\usepackage{tikz,pgfplots}
\usetikzlibrary{patterns}
\usepgfplotslibrary{fillbetween}
\begin{document}

\begin{center}
\begin{tikzpicture}

\begin{axis}[%
width=6cm,
height=5cm,
at={(0cm,0cm)},
scale only axis,
separate axis lines,
every outer x axis line/.append style={black},
every x tick label/.append style={font=\color{black}},
xmin=0,
xmax=2.501,
every outer y axis line/.append style={black},
every y tick label/.append style={font=\color{black}},
ymin=0,
ymax=2.501,
axis background/.style={fill=white}
]

\addplot [color=red,name path=A]
table[row sep=crcr]{%
    0                   0.6\\
    1                   1\\
    2.5109328918269 1.38577280784458\\
};
\addplot [color=blue,name path=B]
table[row sep=crcr]{%
    0                   -1\\
    0.587895504187662   0\\
    1                   1\\
    1.38809536149591    2.5121495529328\\
};

\addplot fill between[
   of=A and B,
   split, 
   every segment no 0/.style={pattern=north east lines},
   every segment no 1/.style={fill=none}];
\end{axis}
\end{tikzpicture}%
\end{center}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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