4

In the minimum working example below I use the keys xmin, xmax, ymin and ymax for calculations of coordinates for annotations. However, this only works when the axis limits are set explicitly in the axis options.

I also want the code to work when I do not explicitly set the axis limits. In what key names are the axis limits stored in that case?

Minimum working example:

% 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{\slopeTriangle}[9]
{
    % Calculate auxilliary quantities.
    \pgfmathsetmacro{\xA}{#6+(#1+#2)*(#7-#6)}
    \pgfmathsetmacro{\yA}{#8+#3*(#9-#8)}
    \pgfmathsetmacro{\xB}{#6+#1*(#7-#6)}
    \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=-0.1, % WHAT IF I REMOVE THIS?
            xmax=1.1, % WHAT IF I REMOVE THIS?
            xlabel=$x$,
            ytick={-0.2,0,2,2.2},
            ymin=-0.2, % WHAT IF I REMOVE THIS?
            ymax=2.2, % WHAT IF I REMOVE THIS?
            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};

            \pgfkeysgetvalue{/pgfplots/xmin}{\xmin} % IF THE xmin IS NOT SET EXPLICITLY, THEN WHAT IS THE KEYNAME?
            \pgfkeysgetvalue{/pgfplots/xmax}{\xmax} % IF THE xmax IS NOT SET EXPLICITLY, THEN WHAT IS THE KEYNAME?
            \pgfkeysgetvalue{/pgfplots/ymin}{\ymin} % IF THE ymin IS NOT SET EXPLICITLY, THEN WHAT IS THE KEYNAME?
            \pgfkeysgetvalue{/pgfplots/ymax}{\ymax} % IF THE ymax IS NOT SET EXPLICITLY, THEN WHAT IS THE KEYNAME?

            \slopeTriangle{0.8}{0.1}{0.1}{1}{blue}{\xmin}{\xmax}{\ymin}{\ymax};
        \end{axis}
    \end{tikzpicture}
\end{document}
Adriaan
  • 3,675
  • 2
  • 21
  • 41
  • Try \pgfmathfloattofixed{\pgfplots@xmin}\xdef\xmin{\pgfmathresult}. However these numbers represent the limits of data instead of axis. – Symbol 1 May 13 '15 at 03:58
  • @Symbol1 Your suggestion does not seem to work. The command \pgfplots@xmin does not seem to exist. Did you perhaps test it? Can you perhaps change my minimum working example accordingly? In the meanwhile, I found out that the commands current axis.south west and current axis.north east could be useful in retrieving the xmin, xmax, ymin and ymax. However, I cannot seem to be able to extract the x and y from these current axis nodes. The answer as given in http://tex.stackexchange.com/questions/14021/extract-the-x-y-part-from-a-coordinate-in-pgfplots does not seem to work. – Adriaan May 13 '15 at 08:41
  • 1
    The calculated values for the axis limits and scale are not available until \end{axis}. hence the use of \pgfplotsextra{}. – John Kormylo May 13 '15 at 13:57

1 Answers1

3

According to the pgfplots reference for /pgfplots/xmin, it holds

During the visualization phase, i.e. during \end{axis}, these keys will be set to the final axis limits. You can access the values by means of \pgfkeysvalueof{/pgfplots/xmin}

Consequently, you only need to enclose your computation and node generation by \pgfplotsextra and your code receives the expected (automatically computed) values. I added a suitable title to show those values (for debugging purposes):

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

\newcommand{\slopeTriangle}[9]
{
    % Calculate auxilliary quantities.
    \pgfmathsetmacro{\xA}{#6+(#1+#2)*(#7-#6)}
    \pgfmathsetmacro{\yA}{#8+#3*(#9-#8)}
    \pgfmathsetmacro{\xB}{#6+#1*(#7-#6)}
    \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=-0.1, % WHAT IF I REMOVE THIS?
            %xmax=1.1, % WHAT IF I REMOVE THIS?
            xlabel=$x$,
            ytick={-0.2,0,2,2.2},
            %ymin=-0.2, % WHAT IF I REMOVE THIS?
            %ymax=2.2, % WHAT IF I REMOVE THIS?
            ylabel style={rotate=-90},
            ylabel=$y$,
            unit vector ratio=2 1 1,
            clip=false,
            % Show (automatically) computed limits:
            title={%
                 Axis limits are
                 $
                [\pgfmathprintnumber{\pgfkeysvalueof{/pgfplots/xmin}}
                :\pgfmathprintnumber{\pgfkeysvalueof{/pgfplots/xmax}}
                ] \times
                [\pgfmathprintnumber{\pgfkeysvalueof{/pgfplots/ymin}}
                :\pgfmathprintnumber{\pgfkeysvalueof{/pgfplots/ymax}}
                ]$
            },
        ]
            \addplot[blue,domain=0:1] {x};
            \addplot[red,domain=0:1] {2*x};

            \pgfplotsextra{
            \pgfkeysgetvalue{/pgfplots/xmin}{\xmin}
            \pgfkeysgetvalue{/pgfplots/xmax}{\xmax}
            \pgfkeysgetvalue{/pgfplots/ymin}{\ymin}
            \pgfkeysgetvalue{/pgfplots/ymax}{\ymax}
            \slopeTriangle{0.8}{0.1}{0.1}{1}{blue}{\xmin}{\xmax}{\ymin}{\ymax};
            }
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

  • The use of \pgfplotsextra works great in case of a regular axis environment, but not in case of a loglogaxis, semilogxaxis and semilogyaxis. I posted a new question for this at http://tex.stackexchange.com/questions/245391/wrong-axis-limits-when-using-pgfplotsextra-in-loglog-axis-environment. Do you perhaps know how to solve this? – Adriaan May 17 '15 at 09:55