按 y 标签将 pgfplots 垂直对齐到基线

按 y 标签将 pgfplots 垂直对齐到基线

我想pgfplots通过各自的 y 轴标签垂直对齐一些生成的图。因为我想要一些文本(或数学符号),所以我尝试使用 TikZ 选项baseline=(node.position),但不知何故,我无法访问该ylabel节点。这是一个模型示例,其中的图取自pgfplots 画廊

\documentclass{standalone}
\usepackage{pgfplots,tikz}
\begin{document}
\begin{tikzpicture}[baseline=(ylabel.center)]
  \begin{axis}[
      height=5cm,
      xlabel=Cost,
      ylabel=Error]
    \addplot[color=red,mark=x] coordinates {
      (2,-2.8559703)
      (3,-3.5301677)
      (4,-4.3050655)
      (5,-5.1413136)
      (6,-6.0322865)
      (7,-6.9675052)
      (8,-7.9377747)
    };
  \end{axis}
\end{tikzpicture}
$\Rightarrow$
\begin{tikzpicture}[baseline=(ylabel.center)]
  \begin{axis}[
      height=4cm,
      xmin=-3,   xmax=3,
      ymin=-3,   ymax=3,
      extra x ticks={-1,1},
      extra y ticks={-2,2},
      extra tick style={grid=major},
      xlabel=x,
      ylabel=y,
    ]
    \draw[red] \pgfextra{
      \pgfpathellipse{\pgfplotspointaxisxy{0}{0}}
             {\pgfplotspointaxisdirectionxy{1}{0}}
             {\pgfplotspointaxisdirectionxy{0}{2}}
    };
    \draw[blue] \pgfextra{
      \pgfpathellipse{\pgfplotspointaxisxy{0}{0}}
             {\pgfplotspointaxisdirectionxy{1}{1}}
             {\pgfplotspointaxisdirectionxy{0}{2}}
    };
    \addplot [only marks,mark=*] coordinates { (0,0) };
  \end{axis}
\end{tikzpicture}
\end{document}

我究竟做错了什么?

答案1

ylabelsylabel默认没有指定名称,但您可以通过在选项ylabel style={name=ylabel}中添加来添加axis

或者,您可以说[baseline=(current axis.east)]而不添加ylabel style,这会产生相同的输出(至少在这种情况下)。

在此处输入图片描述

\documentclass{standalone}
\usepackage{pgfplots,tikz}
\begin{document}
\begin{tikzpicture}[baseline=(ylabel.center)]
  \begin{axis}[
      height=5cm,
      xlabel=Cost,
      ylabel=Error,
      ylabel style={name=ylabel}]
    \addplot[color=red,mark=x] coordinates {
      (2,-2.8559703)
      (3,-3.5301677)
      (4,-4.3050655)
      (5,-5.1413136)
      (6,-6.0322865)
      (7,-6.9675052)
      (8,-7.9377747)
    };
  \end{axis}
\end{tikzpicture}
$\Rightarrow$
\begin{tikzpicture}[baseline=(ylabel.center)]
  \begin{axis}[
      height=4cm,
      xmin=-3,   xmax=3,
      ymin=-3,   ymax=3,
      extra x ticks={-1,1},
      extra y ticks={-2,2},
      extra tick style={grid=major},
      xlabel=x,
      ylabel=y,
      ylabel style={name=ylabel}
    ]
    \draw[red] \pgfextra{
      \pgfpathellipse{\pgfplotspointaxisxy{0}{0}}
             {\pgfplotspointaxisdirectionxy{1}{0}}
             {\pgfplotspointaxisdirectionxy{0}{2}}
    };
    \draw[blue] \pgfextra{
      \pgfpathellipse{\pgfplotspointaxisxy{0}{0}}
             {\pgfplotspointaxisdirectionxy{1}{1}}
             {\pgfplotspointaxisdirectionxy{0}{2}}
    };
    \addplot [only marks,mark=*] coordinates { (0,0) };
  \end{axis}
\end{tikzpicture}
\end{document}

相关内容