PGFplots 中缺少绘图实例

PGFplots 中缺少绘图实例

我编写了一个小宏,用于在PGFplots轴环境中绘制带斜率的三角形。我尝试绘制两个这样的三角形。但是,由于某种原因,第一个三角形不见了。

我该如何解决这个问题?

最小工作示例:

% Mind section '4.17 Custom annotations' of the PGFplots manual Revision 1.12 (2015/01/31).
\documentclass[margin=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{calc}

\newcommand{\xmin}{-0.1}
\newcommand{\xmax}{1.1}
\newcommand{\ymin}{-0.2}
\newcommand{\ymax}{2.2}

\newcommand{\slopeTriangle}[5]
{
    % Calculate auxilliary quantities.
    \pgfmathsetmacro{\xA}{\xmin+(#1+#2)*(\xmax-\xmin)}
    \pgfmathsetmacro{\yA}{\ymin+#3*(\ymax-\ymin)}
    \pgfmathsetmacro{\xB}{\xmin+#1*(\xmax-\xmin)}
    \pgfmathsetmacro{\yB}{\yA}
    \pgfmathsetmacro{\xC}{\xA}
    \pgfmathsetmacro{\yC}{\yA+(\xA-\xB)*#4}

    % Define coordinates for \draw.
    \coordinate (A) at (axis cs:\xA,\yA);
    \coordinate (B) at (axis cs:\xB,\yB);
    \coordinate (C) at (axis cs:\xC,\yC);

    % Draw slope triangle.
    \draw[#5] (A)--(B) node[pos=0.5,anchor=north] {1};
    \draw[#5] (B)--(C);
    \draw[#5] (C)--(A) node[pos=0.5,anchor=west] {1};
}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}
        [
            xtick={-0.1,0,1,1.1},
            xmin=\xmin,
            xmax=\xmax,
            xlabel=$x$,
            ytick={-0.2,0,2,2.2},
            ymin=-0.2,
            ymax=2.2,
            ylabel style={rotate=-90},
            ylabel=$y$,
            unit vector ratio=2 1 1,
            clip=false
        ]
            \addplot[blue,domain=0:1] {x};
            \addplot[red,domain=0:1] {2*x};

            \slopeTriangle{0.8}{0.1}{0.1}{1}{blue}; % WHY IS THIS TRIANGLE NOT DRAWN?   
            \slopeTriangle{0.65}{0.1}{0.1}{2}{red};
        \end{axis}
    \end{tikzpicture}
\end{document}

答案1

axis环境中,所有“正常”的 TikZ 命令在首次遇到时都会被收集,并且仅在轴完成时在稍后阶段执行。此时,辅助宏包含第二个三角形的值,因此第二个三角形被绘制两次,而第一个三角形根本没有被绘制。您可以通过将三角形代码包装在 中来解决这个问题\pgfplotsextra{...}

\documentclass[margin=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{calc}

\newcommand{\xmin}{-0.1}
\newcommand{\xmax}{1.1}
\newcommand{\ymin}{-0.2}
\newcommand{\ymax}{2.2}

\newcommand{\slopeTriangle}[5]
{
    \pgfplotsextra{
    % Calculate auxilliary quantities.
    \pgfmathsetmacro{\xA}{\xmin+(#1+#2)*(\xmax-\xmin)}
    \pgfmathsetmacro{\yA}{\ymin+#3*(\ymax-\ymin)}
    \pgfmathsetmacro{\xB}{\xmin+#1*(\xmax-\xmin)}
    \pgfmathsetmacro{\yB}{\yA}
    \pgfmathsetmacro{\xC}{\xA}
    \pgfmathsetmacro{\yC}{\yA+(\xA-\xB)*#4}

    % Define coordinates for \draw.
    \coordinate (A) at (axis cs:\xA,\yA);
    \coordinate (B) at (axis cs:\xB,\yB);
    \coordinate (C) at (axis cs:\xC,\yC);

    % Draw slope triangle.
    \draw[#5] (A)--(B) node[pos=0.5,anchor=north] {1};
    \draw[#5] (B)--(C);
    \draw[#5] (C)--(A) node[pos=0.5,anchor=west] {1};
    }
}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}
        [
            xtick={-0.1,0,1,1.1},
            xmin=\xmin,
            xmax=\xmax,
            xlabel=$x$,
            ytick={-0.2,0,2,2.2},
            ymin=-0.2,
            ymax=2.2,
            ylabel style={rotate=-90},
            ylabel=$y$,
            unit vector ratio=2 1 1,
            clip=false
        ]
            \addplot[blue,domain=0:1] {x};
            \addplot[red,domain=0:1] {2*x};

            \slopeTriangle{0.8}{0.1}{0.1}{1}{blue}; % WHY IS THIS TRIANGLE NOT DRAWN?   
            \slopeTriangle{0.65}{0.1}{0.1}{2}{red};
        \end{axis}
    \end{tikzpicture}
\end{document}

相关内容