[Tex/LaTex] Overlay xticklabel and yticklabel in pgfplots

pgfplotstickstikz-pgf

In the picture below I want to overlay the xticklabel $a$ and yticklabel $f(a)$ in beamer. Is it possible to overlay ticks labels?

If it's not possible, I can place de labels as nodes, but in that case, what are the defalult position of xticklabes and yticklabes in pgfplots?
Thanks.

\documentclass{beamer}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    axis x line=middle,
    axis y line=middle,
    xtick={1},
    xticklabels={$a$},
    ytick={1},
    yticklabels={$f(a)$}, 
    ]
    \addplot+[domain=0:1.5, mark=none, smooth] {x^2} node[anchor=south] {$f(x)$};
    \coordinate (O) at (0,0);
    \coordinate (A) at (1,1);
    \fill (A) circle (1.2pt);
    \draw[gray, dotted] (A) -- (A|-O);
    \draw[gray, dotted] (A) -- (A-|O);
  \end{axis}
\end{tikzpicture}
\end{document}

Best Answer

Your plot has two problems at the moment:

  • Clipping cuts away the name of the function, f(x) (and would cut away any nodes that you place outside). To avoid this, add clip=false to the options of the axis environment.
  • The coordinates are currently defined in tikzpicture dimensions, not in the coordinate system of the axis environment. Replace (0,0) by (axis cs:0,0) and (1,1) by (axis cs:1,1). Then your gray dotted lines will appear.

If I interpret your question correctly, you want to have the labels appear gradually frame by frame. I don't think that pgfplots is able to cooperate with beamer directly, so you will need \node and \only commands.

The plot will have different size in different frames, so it will jump. Therefore we have to set the bounding box explicitly to make it contain the plot in all frames.

The following output is generated from the code below.

enter image description hereenter image description hereenter image description here

\documentclass{beamer}
\usepackage{pgfplots}
\setbeamertemplate{navigation symbols}{} % Has anyone ever used these to navigate?
\begin{document}
\begin{frame}
\begin{tikzpicture}
  \begin{axis}[
    axis x line=middle,
    axis y line=middle,
    xtick={1},
    xticklabels={},
    ytick={1},
    yticklabels={},
    clip=false
    ]
    \addplot+[domain=0:1.5, mark=none, smooth] {x^2} node[anchor=south] {$f(x)$};
    \coordinate (O) at (axis cs:0,0);
    \coordinate (A) at (axis cs:1,1);
    \fill (A) circle (1.2pt);
    \draw[gray, dotted] (A) -- (A|-O);
    \draw[gray, dotted] (A) -- (A-|O);
    \only<2->{\draw (axis cs:1,-0.03) node[below]{$a$};}
    \only<3->{\draw (axis cs:-0.0164,1) node[left]{$f(a)$};}
  \end{axis}
  \useasboundingbox (-1,-1) rectangle (current axis.above north east);
\end{tikzpicture}
\end{frame}
\end{document}

Edit: Here is another solution that relies on pgfplots to position the labels. It is probably a better solution than the one above as long as it is possible to parametrize the plot by macros. It still needs \useasboundingbox to avoid that the plot jumps.

\documentclass{beamer}
\usepackage{pgfplots}
\setbeamertemplate{navigation symbols}{} % Has anyone ever used these to navigate?
\begin{document}
\newcommand\xlabel{} % Check whether definable and initialize
\newcommand\ylabel{} % Check whether definable and initialize
\begin{frame}
\only<2->{\renewcommand\xlabel{$a$}}
\only<3->{\renewcommand\ylabel{$f(a)$}}
\begin{tikzpicture}
  \begin{axis}[
    axis x line=middle,
    axis y line=middle,
    xtick={1},
    xticklabels={\xlabel},
    ytick={1},
    yticklabels={\ylabel},
    clip=false
    ]
    \addplot+[domain=0:1.5, mark=none, smooth] {x^2} node[anchor=south] {$f(x)$};
    \coordinate (O) at (axis cs:0,0);
    \coordinate (A) at (axis cs:1,1);
    \fill (A) circle (1.2pt);
    \draw[gray, dotted] (A) -- (A|-O);
    \draw[gray, dotted] (A) -- (A-|O);
  \end{axis}
  \useasboundingbox (-1.3,-1) rectangle (current axis.above north east);
\end{tikzpicture}
\end{frame}
\end{document}