4

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}
\pgfplotsset{compat=newest}
\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}
\addplot table {data.dat};
\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}

MWE

1 Answers1

3

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}
\pgfplotsset{compat=newest}
\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}
        \addplot table {data.dat};
        \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}

enter image description here

Kpym
  • 23,002