在 PGFplots 对数轴环境中用斜率注释/绘制三角形

在 PGFplots 对数轴环境中用斜率注释/绘制三角形

作为对发布在在 PGFplots 轴环境中注释/绘制带斜率的三角形,我如何在中执行此操作loglogaxis

因此,给定中的相对位置loglogaxis和斜率,在中的相对位置绘制具有该斜率的三角形loglogaxis

此类图表/注释经常出现在科学计算、数值分析、有限元方法等期刊论文中。例如让 PGFplot 对齐绘图之间的 \draw 命令,但图形的某个角上有三角形。

答案1

您可以使用以下代码进行操作:https://tex.stackexchange.com/a/245685/28093

\documentclass[margin=1cm]{standalone}
\usepackage{amsmath}
\usepackage{pgfplots}

\pgfplotsset{compat=newest}

\usetikzlibrary{calc}

%%% START MACRO FOR ANNOTATION OF TRIANGLE WITH SLOPE %%%.
\newcommand{\logLogSlopeTriangle}[5]
{
    % #1. Relative offset in x direction.
    % #2. Width in x direction, so xA-xB.
    % #3. Relative offset in y direction.
    % #4. Slope d(y)/d(log10(x)).
    % #5. Plot options.

    \pgfplotsextra
    {
        \pgfkeysgetvalue{/pgfplots/xmin}{\xmin}
        \pgfkeysgetvalue{/pgfplots/xmax}{\xmax}
        \pgfkeysgetvalue{/pgfplots/ymin}{\ymin}
        \pgfkeysgetvalue{/pgfplots/ymax}{\ymax}

        % Calculate auxilliary quantities, in relative sense.
        \pgfmathsetmacro{\xArel}{#1}
        \pgfmathsetmacro{\yArel}{#3}
        \pgfmathsetmacro{\xBrel}{#1-#2}
        \pgfmathsetmacro{\yBrel}{\yArel}
        \pgfmathsetmacro{\xCrel}{\xArel}
        %\pgfmathsetmacro{\yCrel}{ln(\yC/exp(\ymin))/ln(exp(\ymax)/exp(\ymin))} % REPLACE THIS EXPRESSION WITH AN EXPRESSION INDEPENDENT OF \yC TO PREVENT THE 'DIMENSION TOO LARGE' ERROR.

        \pgfmathsetmacro{\lnxB}{\xmin*(1-(#1-#2))+\xmax*(#1-#2)} % in [xmin,xmax].
        \pgfmathsetmacro{\lnxA}{\xmin*(1-#1)+\xmax*#1} % in [xmin,xmax].
        \pgfmathsetmacro{\lnyA}{\ymin*(1-#3)+\ymax*#3} % in [ymin,ymax].
        \pgfmathsetmacro{\lnyC}{\lnyA+#4*(\lnxA-\lnxB)}
        \pgfmathsetmacro{\yCrel}{\lnyC-\ymin)/(\ymax-\ymin)} % THE IMPROVED EXPRESSION WITHOUT 'DIMENSION TOO LARGE' ERROR.

        % Define coordinates for \draw. MIND THE 'rel axis cs' as opposed to the 'axis cs'.
        \coordinate (A) at (rel axis cs:\xArel,\yArel);
        \coordinate (B) at (rel axis cs:\xBrel,\yBrel);
        \coordinate (C) at (rel axis cs:\xCrel,\yCrel);

        % Draw slope triangle.
        \draw[#5]   (A)-- node[pos=0.5,anchor=north] {1}
                    (B)-- 
                    (C)-- node[pos=0.5,anchor=west] {#4}
                    cycle;
    }
}
%%% END MACRO FOR ANNOTATION OF TRIANGLE WITH SLOPE %%%.

\begin{document}
    \begin{tikzpicture}
        \begin{loglogaxis}
        [
            xlabel=$x$,
            ylabel style={rotate=-90},
            ylabel=$y$,
            legend style=
            {
                at={(1,1)},
                anchor=north west,
                draw=none,
                fill=none
            },
            legend cell align=left,
            grid=major,
            clip=false
        ]
            \addplot[blue,line width=1pt,domain=10^1:10^4] {sqrt(x)};
            \addplot[red,line width=1pt,domain=10^1:10^4] {x};
            \addplot[green!75!black,line width=1pt,domain=10^1:10^4] {x^2};
            \addplot[brown,line width=1pt,domain=10^1:10^4] {x^3};

            \logLogSlopeTriangle{0.9}{0.1}{0.16}{0.5}{blue};
            \logLogSlopeTriangle{0.9}{0.1}{0.29}{1}{red};
            \logLogSlopeTriangle{0.9}{0.1}{0.53}{2}{green!75!black};
            \logLogSlopeTriangle{0.9}{0.1}{0.79}{3}{brown};

            \legend
            {
                $\sqrt{x}$,
                $x$,
                $x^2$,
                $x^3$
            }
        \end{loglogaxis}
    \end{tikzpicture}
\end{document}

结果:

相关内容