`loglogaxis` 图中“维度太大”

`loglogaxis` 图中“维度太大”

我构建了一个宏,通过 中的三角形注释图形的斜率loglogaxis。对于小斜率,它工作正常,但对于大斜率,我得到一个Dimension too large错误。

最小工作示例:

\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.
        \pgfmathsetmacro{\xA}{(exp(\xmin))^(1-#1)*(exp(\xmax))^#1}
        \pgfmathsetmacro{\yA}{(exp(\ymin))^(1-#3)*(exp(\ymax))^#3}
        \pgfmathsetmacro{\xB}{(exp(\xmin))^(1-(#1-#2))*(exp(\xmax))^(#1-#2)}
        \pgfmathsetmacro{\yB}{\yA}
        \pgfmathsetmacro{\xC}{\xA}
        \pgfmathsetmacro{\yC}{\yA/(\xB^#4)*\xA^#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)-- 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$,
            grid=major,
            clip=false
        ]
            \addplot[blue,line width=1pt,domain=10^1:10^2] {sqrt(x)};
            \addplot[red,line width=1pt,domain=10^1:10^2] {x};
            %\addplot[green!75!black,line width=1pt,domain=10^1:10^2] {x^3}; % RESULTS IN A 'DIMENSION TOO LARGE ERROR. HOW DO I FIX THIS?'

            \logLogSlopeTriangle{0.9}{0.1}{0.1}{0.5}{blue};
            \logLogSlopeTriangle{0.75}{0.1}{0.1}{1}{red};
            %\logLogSlopeTriangle{0.6}{0.1}{0.1}{3}{green!75!black}; % RESULTS IN A 'DIMENSION TOO LARGE ERROR. HOW DO I FIX THIS?'
        \end{loglogaxis}
    \end{tikzpicture}
\end{document}

结果:

取消注释\addplot ... {x^3}及其对应的注释\logLogSlopeTriangle{...},我收到Dimension too large错误。

我该如何解决?

答案1

解决方案是使用相对坐标代替绝对坐标。因此,rel axis cs使用 代替axis cs

\yCrel如果不使用的话,要推导出表达式需要相当多的数学知识\yC,但这是结果(参见代码),并且它可以正常工作而没有出现错误Dimension too large

\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}

结果:

相关内容