4

I am trying to create a dynamic plot in a beamer presentation using TikZ and pgfplots. The goal is to gradually display elements across multiple slides:

  1. Slide 1: Display an empty coordinate system.
  2. Slide 2: Show a parabolic function (f(x) = x^2).
  3. Slide 3: Add a point of interest annotation.

However, the animation effect is not working as expected. Instead of showing the elements step by step, everything appears in the first frame, including the function and the point of interest. Here is the code I used:

 \documentclass{beamer} \usepackage{tikz,pgfplots} \begin{document} \begin{frame}{Dynamic Plot with Onslide} \begin{tikzpicture} \begin{axis}[ axis lines = middle, xlabel = $x$, ylabel = {$f(x)$}, domain = -2:2, samples = 100, % Smooth curve height=7cm, % Ensure consistent size width=7cm ] % Slide 1: Invisible function to stabilize the axis \onslide<1>{ \addplot[color=white]{x^2}; } % Slide 2: Actual function is visible \onslide<2->{ \addplot[color=blue]{x^2}; } % Slide 3: Add the point of interest \onslide<3>{ \node[above] at (axis cs:1,1) {Point of interest}; } \end{axis} \end{tikzpicture} \end{frame} \end{document} 

Issue:

Despite using \onslide, all elements are displayed in the first frame, rather than appearing progressively across slides. I suspect it may be related to how pgfplots handles rendering within beamer.


Additional Context:

I have read that \onslide or similar commands might need to work within a single axis environment or require special handling for animations in pgfplots. Is there a simpler or more reliable way to achieve the desired effect?

How can I fix this issue so the animation renders correctly, with the function and annotations appearing only on their respective slides?

2
  • 1
    please can you make your example ready-to-compile? Commented Dec 17, 2024 at 1:32
  • It can be compiled \documentclass{beamer} \usepackage{tikz,pgfplots} \begin{document} ... \end{document} Commented Dec 17, 2024 at 3:03

3 Answers 3

3
  • I instead of repeating your plot two times, you could simplify the code by changing the colour from the second overlay onwards

  • TikZ macros like \node are overlay-aware. This means you can use \node<3-> ... to add your node from the 3rd overlay onwards


\documentclass{beamer} \usepackage{tikz,pgfplots} \usebeamercolor{background canvas} \usetikzlibrary{overlay-beamer-styles} \begin{document} \begin{frame} \frametitle{Dynamic Plot with Onslide} \begin{tikzpicture} \begin{axis}[ axis lines = middle, xlabel = $x$, ylabel = {$f(x)$}, domain = -2:2, samples = 100, % Smooth curve height=7cm, % Ensure consistent size width=7cm ] % Slide 1: Invisible function to stabilize the axis \addplot[alt=<2->{color=blue}{color={background canvas.bg}}]{x^2}; % Slide 3: Add the point of interest \node<3->[above] at (axis cs:1,1) {Point of interest}; \end{axis} \end{tikzpicture} \end{frame} \end{document} 

enter image description here

5

This is a slimmed-down version of a simple custom package I use to facilitate overlay-sensitive specifications in tikz, forest and prooftrees. The techniques work equally well with pgfplots.

axis-plot-annotation

\documentclass{beamer} \usepackage{pgfplots} \pgfplotsset{compat=1.18} \makeatletter \AddToHook{begindocument/before}{% \tikzset{% visible on/.style={alt=#1{}{invisible}}, opaque on/.style={alt=#1{}{transparent}}, alert on/.style={alt=#1{alerted}{}}, }% \tikzset{% set up for transitions using tikz with beamer overlays - developed by Daniel (http://tex.stackexchange.com/a/55849/) and, in earlier form, by Matthew Leingang (http://tex.stackexchange.com/a/6155/) and modified for this use, I think by Qrrbrbirlbel (http://tex.stackexchange.com/a/112471/) invisible/.style={opacity=0,text opacity=0}, alt/.code args={<#1>#2#3}{% \alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}} % \pgfkeysalso doesn't change the path }, transparent/.style={opacity=0.1,text opacity=0.1}, alerted/.style={color=alerttextdefaultfg}, }% } \makeatother \begin{document} \begin{frame}{Dynamic Plot with Onslide} \begin{tikzpicture} \begin{axis}[ axis lines = middle, xlabel = $x$, ylabel = {$f(x)$}, domain = -2:2, samples = 100, % Smooth curve height=7cm, % Ensure consistent size width=7cm ] % Slide 1: Invisible function to stabilize the axis \addplot[color=white,visible on=<1>]{x^2}; % Slide 2: Actual function is visible \addplot[color=blue,visible on=<2->]{x^2}; % Slide 3: Add the point of interest \node[above,visible on=<3>] at (axis cs:1,1) {Point of interest}; \end{axis} \end{tikzpicture} \end{frame} \end{document} 

Because the documentation for magick is so appalling one wonders they bother at all: magick -density 300 <compiled pdf> <output file> ; magick <image 1> <image 2> ... <image k> +append <output file>

3
  • I've successfully fixed the dynamic plot issue in my Beamer presentation. Your solution worked perfectly. Thank you so much for your help! Commented Dec 17, 2024 at 3:58
  • 1
    The TikZ library overlay-beamer-styles includes the alt key and a few others like it. Not that one needs a library. But if one would rather use one, it's there. Commented Dec 17, 2024 at 9:53
  • 1
    @MatthewLeingang ah, interesting. thanks. I have a fairly customised version of this at this point, but that is certainly good to know. Commented Dec 17, 2024 at 16:53
3

If you would like to stick to your syntax, you could use \onslide* or \only instead of \onslide. This will ensure that the content isn't present on prior overlays:

\documentclass{beamer} \usepackage{tikz,pgfplots} \begin{document} \begin{frame} \frametitle{Dynamic Plot with Onslide} \begin{tikzpicture} \begin{axis}[ axis lines = middle, xlabel = $x$, ylabel = {$f(x)$}, domain = -2:2, samples = 100, % Smooth curve height=7cm, % Ensure consistent size width=7cm ] % Slide 1: Invisible function to stabilize the axis \onslide*<1>{ \addplot[color=white]{x^2}; } % Slide 2: Actual function is visible \onslide*<2->{ \addplot[color=blue]{x^2}; } % Slide 3: Add the point of interest \onslide*<3->{ \node[above] at (axis cs:1,1) {Point of interest}; } \end{axis} \end{tikzpicture} \end{frame} \end{document} 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.