4

I'd like to show a function using different values for it's domain, like "zooming out" over a series of frames. Using the following code, both the text before the plot and the plot's axes/frame jump around across the frames:

\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}

\begin{document}

\begin{frame}
    text

    \begin{center}
        \begin{tikzpicture}
            \begin{axis}[
                    scaled ticks=false,
                    ticklabel style={/pgf/number format/fixed,/pgf/number format/1000 sep={\,}},
                    width=11cm,
                    height=6cm
                ]
                \only<1>{ \addplot [domain=0:5] {x^3}; }
                \only<2>{ \addplot [domain=0:15] {x^3}; }
                \only<3>{ \addplot [domain=0:100] {x^3}; }
                \only<4>{ \addplot [domain=0:1000] {x^3}; }
            \end{axis}
        \end{tikzpicture}
    \end{center}
\end{frame}

\end{document}

Note that the different slides should actually change their x axis's maximum, ie. the first slide should show the function with the domain 0:5 scaled to the plot's width. I need this since the "zooming out" effect is required to show some details of the function that start to be invisible in the larger domains.

Avoiding jumping frames in beamer suggests using overlayarea, with which I was able to avoid the jumping text, but doesn't change anything for the plot's contents.

Is there any way to force all addplot commands to produce the same size, ideally with the plots' (0,0) points being in the same place across all frames?

6

This what the overlay-beamer-styles edit is for. In addition, I like the [t] option, which does not help here, but in general it helps avoiding jumps when working with \only. In addition I gave the y ticks a fixed width. (Your plot is a bit too wide.)

\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{overlay-beamer-styles}
\begin{document}

\begin{frame}[t]% <- I prefer [t] to avoid jumps 
    text

   \begin{center}
        \begin{tikzpicture}
            \pgfplotsset{scaled ticks=false,ytick style={text
            width=2cm,align=right},
                    ticklabel style={/pgf/number format/fixed,/pgf/number format/1000 sep={\,}},
                    width=11cm,
                    height=6cm}
            \begin{axis}[visible on=<1>]
                 \addplot [domain=0:5] {x^3}; 
            \end{axis}
            \begin{axis}[visible on=<2>]
                 \addplot [domain=0:15] {x^3}; 
            \end{axis}
            \begin{axis}[visible on=<3>]
                 \addplot [domain=0:100] {x^3}; 
            \end{axis}
            \begin{axis}[visible on=<4>]
                 \addplot [domain=0:1000] {x^3}; 
            \end{axis}

        \end{tikzpicture}
  \end{center}      
\end{frame}

\end{document}

enter image description here

  • Unfortunately, this doesn't preserve the different domains: I'm doing this "zooming out" to be able to see the function's details in the smaller domain(s) that start to be invisible in the larger domain(s). I'll edit the question accordingly. – rainer May 12 at 16:58
  • 2
    @rainer I modified my answer accordingly. – marmot May 12 at 17:09
3

This allows zooming out by fixing the axis size and the bounding box.

\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}

\begin{document}

\begin{frame}
    text

    \begin{center}
        \begin{tikzpicture}
            \path (-2.5cm,-1.5cm) (8.5cm,4.5cm);% bounding box
            \begin{axis}[
                    scaled ticks=false,
                    ticklabel style={/pgf/number format/fixed,/pgf/number format/1000 sep={\,}},
                    width=8cm,
                    height=4cm,
                    scale only axis,
                    %enlargelimits=false% not needed after all
                ]
                \only<1>{ \addplot [domain=0:5] {x^3}; }
                \only<2>{ \addplot [domain=0:15] {x^3}; }
                \only<3>{ \addplot [domain=0:100] {x^3}; }
                \only<4>{ \addplot [domain=0:1000] {x^3}; }
            \end{axis}
        \end{tikzpicture}
    \end{center}
\end{frame}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.