# pgfplots and tikz: clipping a named plot

I would like to clip along a \addplot line made by pgfplots. I am not sure entirely how to do this since my tries have failed. What have done so far is below.

\documentclass[tikz, convert = false]{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat = 1.10}
\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
no marks,
axis x line = center,
axis y line = center,
xmin = 0,
xmax = 5,
xlabel = {x},
ylabel = {y}
]
\addplot[blue, samples = 500, smooth, name path global = pinvx]
gnuplot[domain = .5:5] {1/x};
\addplot[red, samples = 500, smooth, name path global = ninvx]
gnuplot[domain = .5:5] {-1/x};
\legend{$\frac{1}{x}$, $\frac{-1}{x}$}

\path[name path = line1] (axis cs:1.6, 1) -- (axis cs:1.6, -1);
\path[name path = line2] (axis cs:1.75, 1) -- (axis cs:1.75, -1);
\path[name intersections = {of = pinvx and line1, by = P1}];
\path[name intersections = {of = ninvx and line1, by = P2}];
\path[name intersections = {of = pinvx and line2, by = P3}];
\path[name intersections = {of = ninvx and line2, by = P4}];

\draw[green, opacity =.7] (P1) -- (P2);
\draw[green, opacity =.7] (P3) -- (P4);

\begin{scope}
\clip pinvx;
\fill[green, opacity =.7] (P1) -- (P3) -- (1.75, 0) -- (1.6, 0);
\end{scope}
\end{axis}
\end{tikzpicture}
\end{document}


My end goal is to fill the area between the lines and the 1/x and -1/x plots. I tried using clip with the first plot name but that doesn't work.

Here is the plot with out the attempt of shading the area.

-
You might want to have a look at the PGF library fillbetween. –  Henri Menke Apr 22 '14 at 15:45
@HenriMenke that worked thanks. Do you want to make it an answer? –  dustin Apr 22 '14 at 15:58
Answered using fillbetween. –  Henri Menke Apr 22 '14 at 17:43

You can use the fillbetween library of pgfplots, which is pretty straight forward to use:

1. Name the paths e.g. A and B
2. \addplot fill between [of=A and B,...];, where ...stands for other options.

For more information confer to section 5.6 "Fill between" on page 364 of the pgfplots manual.

# Implementation

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
no marks,
axis x line = center,
axis y line = center,
xmin = 0,
xmax = 5,
xlabel = $x$,
ylabel = $y$
]
\addplot[blue, samples = 500, smooth, name path global = pinvx]
gnuplot[domain = .5:5] {1/x};
\addplot[red, samples = 500, smooth, name path global = ninvx]
gnuplot[domain = .5:5] {-1/x};
\legend{$\frac{1}{x}$, $\frac{-1}{x}$}

\addplot fill between [of=pinvx and ninvx,soft clip={domain=1.6:1.75}];
\end{axis}
\end{tikzpicture}
\end{document}


# Output

-

This is is a result by clip. First, you need to define a clip area, whose coordinates are associated to the green lines. Then you draw and fill the two functions with \closedcycle so that the areas below the curve is filled. However, the real resulting clip area are the intersection of aforementioned areas -- The rectangle, areas below 1/x and -1/x.

Code

\documentclass[tikz, convert = false]{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat = 1.8}
\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
no marks,
axis x line = center,
axis y line = center,
xmin = 0,
xmax = 5,
xlabel = {x},
ylabel = {y}
]
\addplot[blue, samples = 500, smooth, name path global = pinvx,domain = .5:5] {1/x};
\addplot[red, samples = 500, smooth, name path global = ninvx, domain = .5:5] {-1/x};
\legend{$\frac{1}{x}$, $\frac{-1}{x}$}

\path[name path = line1] (axis cs:1.6, 1) -- (axis cs:1.6, -1);
\path[name path = line2] (axis cs:1.75, 1) -- (axis cs:1.75, -1);
\path[name intersections = {of = pinvx and line1, by = P1}];
\path[name intersections = {of = ninvx and line1, by = P2}];
\path[name intersections = {of = pinvx and line2, by = P3}];
\path[name intersections = {of = ninvx and line2, by = P4}];

\draw[green, opacity =.7] (P1) -- (P2);
\draw[green, opacity =.7] (P3) -- (P4);

\begin{scope}
\clip (axis cs: 1.6,-1) rectangle (axis cs:1.75,1);
\addplot[samples = 500, smooth, name path global = pinvx,domain = .5:5,fill=green, opacity =.7] {1/x}\closedcycle;
\addplot[samples = 500, smooth, name path global = ninvx, domain = .5:5,fill=green, opacity =.7] {-1/x}\closedcycle;
\end{scope}
\end{axis}
\end{tikzpicture}
\end{document}

-