3

I want to access two spypoints in a tikzpicture environment via node name. Unfortunately, naming the spypoint doesn't seem to work in the same way as it does for the magnifyglass node.

Here is my MWE:

\documentclass{standalone}

\usepackage{pgfplots}
\usetikzlibrary{spy}

\begin{document}

\begin{tikzpicture}

        \begin{scope}[spy using outlines={rectangle, magnification=4, connect spies}]

        \begin{axis}[%
            height=5cm,%
            width=10cm, %
            ymin=-1, ymax=21, %
            xmin=0, xmax=1000, %
            xticklabels={},%
            ytick={0, 10, 20}, %
            name=first,%
            ]

            \coordinate (spypoint1) at (axis cs:30,10);
            \coordinate (magnifyglass1) at (axis cs:300,3.3);

            \coordinate (spypoint2) at (axis cs:850,3.3);
            \coordinate (magnifyglass2) at (axis cs:650,3.4);

        \end{axis}

        \begin{axis}[%
            height=4cm,%
            width=10cm, %
            ymin=-1, ymax=1, %
            xmin=0, xmax=1000, %
                at={(first.below south west)}, yshift=-0.1cm,  anchor=north west, %
            ]

            \coordinate (spypoint3) at (axis cs:30,0);
            \coordinate (magnifyglass3) at (axis cs:300,0.4);

            \coordinate (spypoint4) at (axis cs:850,-0.1);
            \coordinate (magnifyglass4) at (axis cs:600,-0.4);

        \end{axis}

        \spy [black, height=12mm, width=20mm, name=a1] on (spypoint1)
            in node [fill=white,name=a] at (magnifyglass1);

        \spy [black, height=12mm, width=22mm] on (spypoint2)
            in node [fill=white,name=b] at (magnifyglass2);

        \spy [black, height=12mm, width=20mm, name=c1] on (spypoint3)
            in node [fill=white,name=c] at (magnifyglass3);

        \spy [black, height=12mm, width=22mm] on (spypoint4)
            in node [fill=white,name=d] at (magnifyglass4);

        \end{scope}

        \draw[dashed] (a) -- (b);
        \draw[dashed] (c) -- (d);

        \draw[red] (a1) -- (c1);

    \end{tikzpicture}

\end{document}

As you can see, the connections for \draw (a) -- (b) and (c) -- (d) work fine, while the connection (a1) -- (c1) doesn't work. I get the error message

No shape named a1 is known.

and the same for (c1).

Without the \draw[red] (a1) -- (c1); line, the picture looks as follows. The red line resembles what my expected result of adding the line would be

picture with expected line in red

Can anyone tell me the correct way to name the spypoint node? I didn't find anything in the pgfplots or tikz manual. Thank you in advance.

3
  • What do you want to achieve? Connect spypoint1 with spypoint3?
    – d-cmst
    Aug 25, 2016 at 11:25
  • My goal is to connect the lower edges of spypoint1 with the upper edges of spypoint3 and the same for spypoint2 and spypoint4. So I thought, if I could access the nodes via names, I could use something like \draw (spypoint1.south west) -- (spypoint3.north west). Unfortunately I already fail at naming the spypoint nodes as it seems. Aug 25, 2016 at 11:31
  • 2
    Welcome on TeX.SX. According to the last paragraph in section 68.3 in the pgfmanual on page 741 (v3.0.1a) this should be possible using the every spy on node style. Does that help? Aug 25, 2016 at 17:50

2 Answers 2

2

Elaborating on Stefan Pinnow's comment, and using this answer: How can I change the line width of the spy-on node in tikz spy library?, here is how a spy on node can be named (it's quite counterintuitive to modify an every style locally for one node but it's what Tantau suggests in the manual).

        \spy [black, height=12mm, width=20mm, every spy on node/.append style={name=a1}] on (spypoint1)
            in node [fill=white,name=a] at (magnifyglass1);

The complete code:

\documentclass{standalone}

\usepackage{pgfplots}
\usetikzlibrary{spy}

\begin{document}

\begin{tikzpicture}[every spy on node/.style={name}]

        \begin{scope}[spy using outlines={rectangle, magnification=4, connect spies}]

        \begin{axis}[%
            height=5cm,%
            width=10cm, %
            ymin=-1, ymax=21, %
            xmin=0, xmax=1000, %
            xticklabels={},%
            ytick={0, 10, 20}, %
            name=first,%
            ]

            \coordinate (spypoint1) at (axis cs:30,10);
            \coordinate (magnifyglass1) at (axis cs:300,3.3);

            \coordinate (spypoint2) at (axis cs:850,3.3);
            \coordinate (magnifyglass2) at (axis cs:650,3.4);

        \end{axis}

        \begin{axis}[%
            height=4cm,%
            width=10cm, %
            ymin=-1, ymax=1, %
            xmin=0, xmax=1000, %
                at={(first.below south west)}, yshift=-0.1cm,  anchor=north west, %
            ]

            \coordinate (spypoint3) at (axis cs:30,0);
            \coordinate (magnifyglass3) at (axis cs:300,0.4);

            \coordinate (spypoint4) at (axis cs:850,-0.1);
            \coordinate (magnifyglass4) at (axis cs:600,-0.4);

        \end{axis}

        \spy [black, height=12mm, width=20mm, every spy on node/.append style={name=a1}] on (spypoint1)
            in node [fill=white,name=a] at (magnifyglass1);

        \spy [black, height=12mm, width=22mm] on (spypoint2)
            in node [fill=white,name=b] at (magnifyglass2);

        \spy [black, height=12mm, width=20mm, every spy on node/.append style={name=c1}] on (spypoint3)
            in node [fill=white,name=c] at (magnifyglass3);

        \spy [black, height=12mm, width=22mm] on (spypoint4)
            in node [fill=white,name=d] at (magnifyglass4);

        \end{scope}

        \draw[dashed] (a) -- (b);
        \draw[dashed] (c) -- (d);

        \draw[red] (a1) -- (c1);

    \end{tikzpicture}

\end{document} 
0
1

Basically this is the same answer as Christoph Frings' one, but I "optimized" it a bit. The main change is that I introduced the new style Name as a shorthand for every spy on node/.append style. For the rest have a look at the comments in the code.


\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
    \usetikzlibrary{spy}
    \tikzset{
        % define shorthand key/style to give spy-on node names
        Name/.style={
            every spy on node/.append style={
                name=#1,
            },
        },
    }
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            height=5cm,
            width=10cm,
            ymin=-1,ymax=21,
            xmin=0,xmax=1000,
            xticklabels={},
            ytick={0,10,20},
            name=first,
        ]
            \coordinate (spypoint1)     at (axis cs:30,10);
            \coordinate (magnifyglass1) at (axis cs:300,3.3);

            \coordinate (spypoint2)     at (axis cs:850,3.3);
            \coordinate (magnifyglass2) at (axis cs:650,3.4);
        \end{axis}

        \begin{axis}[
            height=4cm,
            width=10cm,
            ymin=-1,ymax=1,
            xmin=0,xmax=1000,
            at={(first.below south west)},
            yshift=-0.1cm,
            anchor=north west,
        ]
            \coordinate (spypoint3)     at (axis cs:30,0);
            \coordinate (magnifyglass3) at (axis cs:300,0.4);

            \coordinate (spypoint4)     at (axis cs:850,-0.1);
            \coordinate (magnifyglass4) at (axis cs:600,-0.4);
        \end{axis}

        % moved begin of scope here
        \begin{scope}[
            spy using outlines={%
                rectangle,
                magnification=4,
                connect spies,
                %
                % moved other common keys here
                black,
                height=12mm,
                width=20mm,
            },
        ]
            \spy [Name=a1] on (spypoint1)
                in node [fill=white,name=a] at (magnifyglass1);

            \spy [width=22mm] on (spypoint2)
                in node [fill=white,name=b] at (magnifyglass2);

            \spy [Name=c1] on (spypoint3)
                in node [fill=white,name=c] at (magnifyglass3);

            \spy [width=22mm] on (spypoint4)
                in node [fill=white,name=d] at (magnifyglass4);

        \end{scope}

        \draw[dashed] (a) -- (b);
        \draw[dashed] (c) -- (d);

        \draw[red] (a1) -- (c1);

    \end{tikzpicture}
\end{document}

Of course this also results in your desired output.

image showing the result of above code

2
  • Thank you very much! This comes in very handy as I will need it at multiple occasions. Aug 26, 2016 at 6:53
  • You are welcome. If you find our answers useful, you could thank us by upvoting our answers (with the upward pointing arrow to the left of it). Aug 26, 2016 at 7:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .