Pgfplots y标签和额外刻度定位问题

Pgfplots y标签和额外刻度定位问题

当我在绘图上添加 y 标签,并在右侧添加带有长描述的额外勾号时,y 标签会向左移动。这似乎是 pgfplots 代码中的布局错误。解决此问题的最佳方法是什么?请注意,在 1.16 版之前不会发生这种情况。

最小示例:

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    extra y ticks = {.5},
    extra y tick labels = {very long desciption},
    extra y tick style  = {ticklabel pos=right},
    ylabel = {y axis label},
    ymin=0, ymax=1, xmin=0, xmax=1,
    ]
 \end{axis}
\end{tikzpicture}

\end{document}

示例图

答案1

我测试了 v1.6,总是得到和你问题中相同的结果。还有这个可以被陈述为错误,但通常人们会将 写在extra tick labels与正常 相同的“一侧” tick labels。因此,目前不存在针对这种非常特殊情况的处理程序……(还有很多其他可能的选项组合,但很难“按预期”处理所有这些组合,而且由于检查所有这些组合,编译速度也会变慢。

请在下方找到可为您提供所需结果的代码。

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{compat=1.3}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        extra y ticks={0.5},
        % put the tick in a `\makebox` thus it does not consume any width
        % and the `ylabel` is positioned right
        extra y tick labels={\makebox[0pt][l]{very long description}},
        extra y tick style={
            ticklabel pos=right,
            % -----------------------------------
            % (needed to adjust the bounding box)
            yticklabel style={
                % add a name to the extra y tick label
                name=ylabel-\ticknum,
            },
            % -----------------------------------
        },
        ylabel={y axis label},
        ymin=0, ymax=1, xmin=0, xmax=1,
    ]
    \end{axis}
        % but then the bounding box doesn't account for the `extra y tick`.
        % Thus, determine the width of it ...
        \pgfmathsetlengthmacro{\MyExtraLabel}{width("very long description")}
        % ... and add that by an (invisible) path
        \path (ylabel-0) -- +(\MyExtraLabel,0);
\end{tikzpicture}
\end{document}

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

相关内容