在轴的倒数第二个刻度上设置单位标签(DIN 461)

在轴的倒数第二个刻度上设置单位标签(DIN 461)

这是在 pgfplot 中设置刻度标签

Stefan 发布的解决方案非常有效。但是当我使用时,y dir=reversesecondlast 标签因此靠近原点(见下面的示例)。

我尝试将其更改\lastyticknum\firstyticknum,但结果相同。有什么想法吗?

\documentclass[a4paper]{article}

\usepackage{siunitx}
\usepackage{pgfplots}
    \pgfplotsset{
        compat=1.5,
        % adjusted from <https://tex.stackexchange.com/a/416810/95441>
        din yunit2/.style={
            yticklabel style={
                name=ylabel\ticknum,
                append after command=\pgfextra{\xdef\firstyticknum{\ticknum}}
            },
            % changed `.code' to `.append code'
            after end axis/.append code={
                \pgfmathparse{int(\firstyticknum-1)}
                % first "overdraw" the node by rectangle, so it isn't seen any more
                \fill [white]
                    (ylabel\pgfmathresult.south west)
                        rectangle
                    % the `xshift' is needed to not overdraw the axis line
                    ([xshift=-0.4pt]ylabel\pgfmathresult.north east);
                % then place the "unit" text
                \node [anchor=base] at (ylabel\pgfmathresult.base) {#1};
            },
        },
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xtick distance=1,
        ytick distance=500,
        din yunit2={\si{\meter}},
        y dir=reverse,
        ylabel={test},
        legend pos={north west}        
    ]
        \addplot [no marks, blue] table [x=a, y=b] {
            a   b
            1   370
            2   -740
            3   -1110
            4   -1480
            5   -1850
            6   -2220
            7   -2590
            8   -2960
            9   -3330
        };
        \legend{test}
    \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

在反向轴上,很明显你想在第二勾选,因此您只需替换ylabel-\pgfmathresult即可ylabel-2使其工作。(请注意,我在勾选标签名称中添加了一个破折号。)

有关详细信息,请查看代码中的注释。

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{siunitx}
\usepackage{pgfplots}
    \pgfplotsset{
        compat=1.5,
        % adjusted from <https://tex.stackexchange.com/a/416810/95441>
        din yunit reverse/.style={
            yticklabel style={
                name=ylabel-\ticknum,
            },
            % changed `.code' to `.append code'
            after end axis/.append code={
                % first "overdraw" the node by rectangle, so it isn't seen any more
                \fill [white]
                    (ylabel-2.south west)
                        rectangle
                    % the `xshift' is needed to not overdraw the axis line
                    ([xshift=-0.4pt]ylabel-2.north east);
                % then place the "unit" text
                \node [anchor=base] at (ylabel-2.base) {#1};
            },
        },
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xtick distance=1,
        ytick distance=500,
        din yunit reverse={\si{\meter}},
        y dir=reverse,
        ylabel={test},
        legend pos={north west}
    ]
        \addplot [no marks, blue] table [x=a, y=b] {
            a   b
            1   370
            2   -740
            3   -1110
            4   -1480
            5   -1850
            6   -2220
            7   -2590
            8   -2960
            9   -3330
        };
        \legend{test}
    \end{axis}
\end{tikzpicture}
\end{document}

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

相关内容