在 pgfplots/TikZ 中不同绘图区域之间绘制线条

在 pgfplots/TikZ 中不同绘图区域之间绘制线条

我想使用下图的坐标将虚线精确地连接到下图中虚线矩形的顶角,这样当矩形框移动时,虚线连接也会相应移动。在下面的例子中,虚线将手动移动,这太费力了……

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{width=1\textwidth,compat=1.9}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\begin{document}

\begin{tikzpicture}
\begin{axis}[%
    name = plot1, 
    width = 0.3\textwidth, height = 0.3\textwidth,
    xtick pos=top, ytick pos=left,
]
\end{axis}%
%
\begin{axis}[%
    name = plot2,
    at = {($(plot1.south west)+(0.0cm,-0.2cm)$)}, anchor=north west,
    width = 0.5\textwidth, height = 0.5\textwidth,
    xmin=0, xmax=40, ymin=0, ymax=40,
    ]
\addplot coordinates{(10,10)}   ;
% draw rectangle around data
\draw[dashed] (axis cs: 8, 8) rectangle (axis cs: 12, 12);
\end{axis}
%
% draw line to top left corner of rectangle
\draw[dashed] (plot1.south west) --   ($(plot2.south west)+(0.8,1.2)$);
% draw line to to top right corner of rectangle
\draw[dashed] (plot1.south east) --   ($(plot2.south west)+(1.2,1.2)$);
%
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

找到了答案,矩形需要定义为一个节点,这样才能使用其锚点。

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{width=1\textwidth,compat=1.9}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\begin{document}

\begin{tikzpicture}
\begin{axis}[%
    name = plot1, 
    width = 0.3\textwidth, height = 0.3\textwidth,
    xtick pos=top, ytick pos=left,
]
\end{axis}%
%
\begin{axis}[%
    name = plot2,
    at = {($(plot1.south west)+(0.0cm,-0.2cm)$)}, anchor=north west,
    width = 0.5\textwidth, height = 0.5\textwidth,
    xmin=0, xmax=40, ymin=0, ymax=40,
    ]
\addplot coordinates{(10,10)}   ;
% draw rectangle around data
\node[rectangle,draw=black, dashed, minimum width = 0.5cm, minimum height = 0.5cm] (rect) at (axis cs: 10, 10) {};
%
\end{axis}
%
% draw line to top left corner of rectangle
\draw[dashed] (plot1.south west) --   ($(rect.north west)$);
%
% draw line to to top right corner of rectangle
\draw[dashed] (plot1.south east) --   ($(rect.north east)$);
%
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容