# How to exclude items from a TikZ Spy

Somewhat related to this question, I would like to exclude items from the magnified area. In the example below, I do not want the pin lines or the orange line to appear in the magnified area. Is this possible?

MWE:

\documentclass[tikz,11pt]{standalone}
\usepackage{filecontents}
\usepackage{pgfplots}
\usetikzlibrary{spy}

\begin{filecontents}{data.dat}
X   Y
0   0
1   1
2   2
3   1
4   0
\end{filecontents}

\tikzstyle{every pin}=[fill=white,draw=black,font=\footnotesize,]
\tikzstyle{point} = [draw,circle,fill=black,scale=0.3]

\begin{document}
\begin{tikzpicture}[spy using outlines = {circle,magnification=6, connect spies}]
\begin{axis}
\node[point,red,pin=above left:P1] (P1) at (axis cs:0.5,0.5) {};
\node[point,cyan,pin=below right:P2] (P2) at (axis cs:0.52,0.52) {};
\node[point,pin=above right:P3] (P3) at (axis cs:2.5,1.5) {};
\node[point,pin=above right:P4] (P4) at (axis cs:3.5,1) {};
\coordinate (spypoint) at (0.5,0.5);
\coordinate (magnifyglass) at (0.5,1.5);

\spy [size=2cm] on (spypoint) in node[fill=white] at (magnifyglass);

\draw[thick,orange] (P1) -- (P2) -- (P3)--(P4);
\end{axis}
\end{tikzpicture}
\end{document}


• It's a super annoying and tedious hack to alter the spy image. I would really draw it twice and clip the second one with the circle if this is a big issue Feb 27, 2016 at 18:35
• @percusse I doubt it is even possible. Feb 29, 2016 at 4:43
• @symbol1 there must be question here somwhere Feb 29, 2016 at 8:03
• Did the tip of percusse help you to solve your issue or do you need further assistance? Jan 16, 2017 at 20:25

To do this you can spy only on a scope and not on the entire image. In this case everything that is drawn outside the spyed scope is not present in the magnified area.

To add the pins after the scope you can use node also.

\documentclass[tikz,11pt]{standalone}
\usepackage{filecontents}
\usepackage{pgfplots}
\usetikzlibrary{spy}

\begin{filecontents}{data.dat}
X   Y
0   0
1   1
2   2
3   1
4   0
\end{filecontents}

\tikzstyle{every pin}=[fill=white,draw=black,font=\footnotesize]
\tikzstyle{point}=[draw,circle,fill=black,scale=0.3]

\begin{document}
\begin{tikzpicture}
% you can spy in a scope
\begin{scope}[spy using outlines = {circle,magnification=6, connect spies}]
\begin{axis}
\node[point,red] (P1) at (axis cs:0.5,0.5) {}; % <-- remove the pin from here
\node[point,cyan] (P2) at (axis cs:0.52,0.52) {}; % <-- remove the pin from here
\node[point,pin=above right:P3] (P3) at (axis cs:2.5,1.5) {};
\node[point,pin=above right:P4] (P4) at (axis cs:3.5,1) {};
\coordinate (spypoint) at (0.5,0.5);
\coordinate (magnifyglass) at (0.5,1.5);
\spy [size=2cm] on (spypoint) in node[fill=white] at (magnifyglass);
\end{axis}
\end{scope}
% drawing outside the spyed scope is not present in the spy node
\node also [pin=above left:P1] (P1); % <-- add the pin later
\node also [pin=below right:P2] (P2); % <-- add the pin later
\draw[thick,orange] (P1) -- (P2) -- (P3)--(P4);
\end{tikzpicture}
\end{document}