如何命名 tikzfigure 中的 spypoint 以通过节点名称访问它

如何命名 tikzfigure 中的 spypoint 以通过节点名称访问它

我想tikzpicture通过节点名称访问环境中的两个监视点。不幸的是,命名监视点的方式似乎与命名放大镜节点的方式不同。

这是我的 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}

如您所见,\draw (a) -- (b)和的连接(c) -- (d)工作正常,而 连接(a1) -- (c1)不起作用。我收到错误消息

未发现名为 a1 的形状。

和 也一样(c1)

如果没有这条\draw[red] (a1) -- (c1);线,图片看起来如下。红线类似于我添加这条线后的预期结果

带有红色预期线的图片

pgfplots有人能告诉我命名 spypoint 节点的正确方法吗?我在手册中没有找到任何内容tikz。提前谢谢您。

答案1

详细说明Stefan Pinnow 的评论,并使用这个答案:如何更改 tikz 间谍库中间谍节点的线宽?,下面是spy on节点的命名方式(为一个节点本地修改every样式是相当违反直觉的,但这是 Tantau 在手册中所建议的)。

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

完整代码:

\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} 

答案2

基本上这与以下答案相同克里斯托弗·弗林斯' 一个,但我对它进行了一些“优化”。主要的变化是我引入了新样式Name作为 的简写every spy on node/.append style。其余的请查看代码中的注释。


\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}

当然这也会产生您想要的输出。

该图显示了上述代码的结果

相关内容