外部坐标破坏了组图的对齐

外部坐标破坏了组图的对齐

这个答案展示了如何使用 绘制与先前图中定义的节点相重叠的图形TikZ。但是,该解决方案(即添加cell picture=if necessary到轴)不适用于groupplots

如果我不使用该选项,我会得到与上面链接的答案解决的相同的问题,并且第二个图的结果甚至更糟:

在此处输入图片描述

向命令添加选项\nextgroupplot(相当于轴语句)解决了部分问题(箭头指向节点),但它破坏了图的对齐:

在此处输入图片描述

问题:如何使用先前图中定义的节点来制作叠加图groupplots

平均能量损失

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary[pgfplots.groupplots]

\begin{document}

  \begin{figure}
  \begin{tikzpicture}[remember picture]
    \begin{groupplot}[group style={group size=2 by 1},ymin=0,ymax=3]
      \nextgroupplot[cell picture=if necessary] \addplot {1};
      \node [fill,red,circle,draw,label={above:A}] (a) at (axis cs:-4,2) {};
      \nextgroupplot[cell picture=if necessary] \addplot {2};
      \node [fill,blue,circle,draw,label={above:B}] (b) at (axis cs:4,1) {};
    \end{groupplot}
  \end{tikzpicture}
  \caption{A \texttt{groupplots} plot}
  \end{figure}

  This arrow should point to A
  \tikz[remember picture,overlay] \draw[red,->] (0,0) -- (a);
  and this arrow should point to B
  \tikz[remember picture,overlay] \draw[red,->] (0,0) -- (b);

\end{document}

答案1

根据 @zeroth 的评论,我能够整理出一组手动对齐的图表。我非常确定代码可以改进(尤其是图形标题的位置),我完全不确定这可能会有什么问题(例如)externalize,但这解决了我的问题。

在此处输入图片描述

\documentclass{article}
\usepackage{pgfplots}

\pgfplotsset{
  every axis/.append style={ymin=0,ymax=3,cell picture=if necessary}
}

\begin{document}

  \begin{figure}
  \begin{tikzpicture}[baseline,remember picture]
    \begin{axis}[name=left column]
      \addplot {1};
      \node [fill,red,circle,draw,label={above:A}] (a) at (axis cs:-4,2) {};
    \end{axis}
    \begin{axis}[at={(left column.below south west)},yshift=-1cm,anchor=north west]
      \addplot {2.5};
      \node [fill,red,circle,draw,label={above:C}] (c) at (axis cs:4,1.5) {};
    \end{axis}
  \end{tikzpicture}
  \hspace{0.15cm}
  \begin{tikzpicture}[baseline,remember picture]
    \begin{axis}[name=right column]
      \addplot {2};
      \node [fill,red,circle,draw,label={above:B}] (b) at (axis cs:4,1) {};
    \end{axis}
    \begin{axis}[at={(right column.below south west)},yshift=-1cm,anchor=north west]
      \addplot {1.5};
      \node [fill,red,circle,draw,label={above:D}] (d) at (axis cs:-4,2.5) {};
    \end{axis}
  \end{tikzpicture}
  \caption{A fake \texttt{groupplots} plot}
  \end{figure}

  This arrow should point to A
  \tikz[remember picture,overlay] \draw[red,->] (0,0) -- (a);
  ; this arrow should point to B
  \tikz[remember picture,overlay] \draw[red,->] (0,0) -- (b);
  ; this arrow should point to C
  \tikz[remember picture,overlay] \draw[red,->] (0,0) -- (c);
  ; and this to D
  \tikz[remember picture,overlay] \draw[red,->] (0,0) -- (d);

\end{document}

相关内容