[Tex/LaTex] Annotate / plot triangle with slope in PGFplots log log axis environment

annotationsmacrospgfplotstikz-pgf

As a follow up question on the question posted at Annotate / plot triangle with slope in PGFplots axis environment, how do I do this in a loglogaxis?

So, given a relative position in the loglogaxis and a slope, a triangle with this slope is plotted at the relative position in the loglogaxis?

Such figures / annotations can often be seen in journal papers on scientific computing, numerical analysis, finite element method, and so on. See for instance Getting PGFplot to align \draw commands between plots, but then with the triangles in some corner of the figure.

Best Answer

You can do so with the code as found at 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}

Result: