获取两个轴上的刻度之间的 pgfplot 单位(DIN 461)

获取两个轴上的刻度之间的 pgfplot 单位(DIN 461)

就像线程一样 获取 pgfplot 自动生成的刻度距离(DIN 461) 已经问过了,我正在寻找一种方法来获取最后两个刻度之间的单位。来自上述线程的解决方案适用于一个轴。我尝试将代码扩展到两个轴,但这仅适用于一个轴。我做错了什么?

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{siunitx}

\pgfplotsset{
    din xunit/.style={
        xticklabel style={
            name=xlabel\ticknum,
            append after command=\pgfextra{\xdef\lastxticknum{\ticknum}}
        },
        after end axis/.code={
            \pgfmathparse{int(\lastxticknum-1)}
            \path (xlabel\lastxticknum.base) -- (xlabel\pgfmathresult.base) node [midway, anchor=base] {#1};
        }
    },
    din yunit/.style={
        yticklabel style={
            name=ylabel\ticknum,
            append after command=\pgfextra{\xdef\lastyticknum{\ticknum}}
        },
        after end axis/.code={
            \pgfmathparse{int(\lastyticknum-1)}
            \path (ylabel\lastyticknum.base) -- (ylabel\pgfmathresult.base) node [midway, anchor=base] {#1};
        }
    }
}

\begin{document}
  \begin{tikzpicture}
    \begin{axis}[din xunit=\si{\second}, din yunit=\si{\meter}]
      \addplot table {
        0 0
        1 3
        2 4
      };
    \end{axis}
  \end{tikzpicture}
\end{document}

答案1

这是因为第二个after end axis/.code覆盖了第一个。因此,更改.code为即可.append code解决问题。

% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{siunitx}
\usepackage{pgfplots}
    \pgfplotsset{
        compat=1.15,
        din xunit/.style={
            xticklabel style={
                name=xlabel\ticknum,
                append after command=\pgfextra{\xdef\lastxticknum{\ticknum}}
            },
            % changed `.code' to `.append code'
            after end axis/.append code={
                \pgfmathparse{int(\lastxticknum-1)}
                \path (xlabel\lastxticknum.base) -- (xlabel\pgfmathresult.base) node [midway, anchor=base] {#1};
            },
        },
        din yunit/.style={
            yticklabel style={
                name=ylabel\ticknum,
                append after command=\pgfextra{\xdef\lastyticknum{\ticknum}}
            },
            % changed `.code' to `.append code'
            after end axis/.append code={
                \pgfmathparse{int(\lastyticknum-1)}
                \path (ylabel\lastyticknum.base) -- (ylabel\pgfmathresult.base) node [midway, anchor=base] {#1};
            },
        },
    }

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        din xunit=\si{\second},
        din yunit=\si{\meter},
    ]
        \addplot table {
            0 0
            1 3
            2 4
        };
    \end{axis}
\end{tikzpicture}
\end{document}

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

答案2

您可能想要包含一起绘制轴的代码:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{siunitx}

\pgfplotsset{
    din label/.style args={#1 and #2}{
        xticklabel style={
            name=xlabel\ticknum,
            append after command=\pgfextra{\xdef\xlastticknum{\ticknum}}
        },
        yticklabel style={
            name=ylabel\ticknum,
            append after command=\pgfextra{\xdef\ylastticknum{\ticknum}}
        },
        after end axis/.code={
            \pgfmathparse{int(\xlastticknum-1)}
            \path (xlabel\xlastticknum.base) -- (xlabel\pgfmathresult.base) node [midway, anchor=base] {#1};
            \pgfmathparse{int(\ylastticknum-1)}
            \path (ylabel\ylastticknum.base) -- (ylabel\pgfmathresult.base) node [midway, anchor=base] {#2};
        }
    }
}


\begin{document}
  \begin{tikzpicture}
    \begin{axis}[din label=\si{\second} and \si{\meter}]
      \addplot table {
        0 0
        1 3
        2 4
      };
    \end{axis}
  \end{tikzpicture}
\end{document}

结果: 在此处输入图片描述

相关内容