Tikz 覆盖与子图,轴和外部化

Tikz 覆盖与子图,轴和外部化

我正在尝试在两个不同的 tikz 图形之间画线,放置在子图环境中,例如左图中小蓝色矩形的角和右图轴环境的角。

样本

由于这两个图形会变得非常大,我需要用 导出它们externalize。因此我还想使用subfig库而不是groupplots

我以为它可以与remember picture和关键字一起使用,但第一次尝试只带来了错误,所以我担心可能会与和overlay发生冲突。我已经找到了一个很好的描述remember pictureexternalize如何引用矩形的角

是否可以像示例图片中所示那样绘制线条?是否可以将externalize和组合使用subfig(如果不行,我更喜欢groupplotsexternalize之前subfig和 否externalize,尽管覆盖图像不需要“外化”)?

这是我的最小示例:

\documentclass[parskip]{scrartcl}
\usepackage{tikz,pgfplots,subfig, lipsum}
% \usetikzlibrary{external}
% \tikzexternalize[figure list=true]
\begin{document}

\lipsum[1]
\begin{figure}[h]
    \centering
    \subfloat[Overview.]{
        \begin{tikzpicture}[trim axis left, trim axis right]
            \begin{axis}[domain=-10:10,x post scale=0.5]
                \addplot+[red,mark=none]{x^2};
                \draw[fill=blue,opacity=0.3] (axis cs:-2,-0.5) rectangle (axis cs:2,5);
            \end{axis}
        \end{tikzpicture}
    }\quad
    \subfloat[Detail.]{
        \begin{tikzpicture}[trim axis left, trim axis right]
            \begin{axis}[xmin=-2,xmax=2,ymin=-0.5,ymax=5,domain=-2:2,mark=none]
                \addplot+[red,mark=none]{x^2};
            \end{axis}
        \end{tikzpicture}
    }
    \caption{Curve.}
\end{figure}
\lipsum[2]

\end{document}

多谢!

答案1

为了完整起见,我提供了一个简短的版本,groupplots其中包含伪造的子标题和可能的外部化。有不同的方法(请参阅问题下的评论),但这对我来说似乎是最简单的。然而,对于原始图形我意识到额外的线条只会让一切变得混乱......

结果

下面是代码:

\documentclass[parskip]{scrartcl}
\usepackage{tikz,pgfplots,subfig, lipsum}
\usetikzlibrary{pgfplots.groupplots}
\usetikzlibrary{fit,intersections,}
\makeatletter
\tikzset{ % fitting node, see http://goo.gl/KOvpQ
  fitting node/.style={
    inner sep=0pt,
    fill=none,
    draw=none,
    reset transform,
    fit={(\pgf@pathminx,\pgf@pathminy) (\pgf@pathmaxx,\pgf@pathmaxy)}
  },
  reset transform/.code={\pgftransformreset}
}
\makeatother
% \usetikzlibrary{external}
% \tikzexternalize[figure list=true]
\begin{document}

\lipsum[1]
\begin{figure}[h]
    \centering
        \begin{tikzpicture}
            \begin{groupplot}[domain=-10:10,group style={group size=2 by 1,vertical sep=0.7cm,horizontal sep=1.05cm,xlabels at=edge bottom,ylabels at=edge left},x label style={font=\footnotesize}]
                \nextgroupplot[x post scale=0.5,xlabel={(a) Overview.},]
                    \addplot+[red,mark=none]{x^2};
                    \draw[fill=blue,opacity=0.3] (axis cs:-2,-0.5) rectangle (axis cs:2,5) node[fitting node] (rect) {};
                \nextgroupplot[xmin=-2,xmax=2,ymin=-0.5,ymax=5,domain=-2:2,mark=none,x post scale=0.9, xlabel={(b) Detail.}]
                    \addplot+[red,mark=none]{x^2};
            \end{groupplot}

            \draw[dashed] (rect.north west) -- (group c2r1.north west);
            \draw[dashed] (rect.south west) -- (group c2r1.south west);
            \path[name path=lineA] (rect.south east) -- (group c2r1.south east);
            \path[name path=lineB] (rect.north east) -- (group c2r1.north east);
            \path[name path=lineC] (group c2r1.north west) -- (group c2r1.south west);

            \draw [dashed,name intersections={of=lineA and lineC, by=pA}] (rect.south east) -- (pA);
            \draw [dashed,name intersections={of=lineB and lineC, by=pB}] (rect.north east) -- (pB);
            \draw [dashed,gray!90,opacity=0.7] (pA) -- (group c2r1.south east);
            \draw [dashed,gray!90,opacity=0.7] (pB) -- (group c2r1.north east);

        \end{tikzpicture}
\end{figure}

\lipsum[2]
\end{document}

相关内容