7

I want to uncover two plots one at a time using a combination of beamer and pgfplots. The MWE below highlights three attempts.

  • The first attempt uses \onslide but doesn't seem to have any noticable effect, the final picture is directly displayed.
  • The second atempt uses \only which sort of works, but since the second plot isn't considered in the bounds for the first plot's creation, we get a jump of some sort.
  • Following this solution I found the idea of visible on style. This looks promising, but it starts with the fill already there. Further it has four slides - which I don't understand why.

So my question is: how can one start with an empty axis and show one step at a time the two graphs and their respective fills.

\documentclass{beamer}

\usepackage{pgfplots}


\tikzset{
    invisible/.style={opacity=0},
    visible on/.style={alt={#1{}{invisible}}},
    alt/.code args={<#1>#2#3}{%
      \alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}} % \pgfkeysalso doesn't change the path
    },
  }

\begin{document}

\begin{frame}{Attempt 1}
\begin{tikzpicture} 
    \begin{axis}
        \onslide<+->{\addplot[black, fill=red, fill opacity=0.2]{-x+3}\closedcycle;}
        \onslide<+->{\addplot[black, fill=red, fill opacity=0.2]{x}\closedcycle;}
    \end{axis} 
\end{tikzpicture}%
\end{frame}

\begin{frame}{Attempt 2}
\begin{tikzpicture} 
    \begin{axis}
        \only<+->{\addplot[black, fill=red, fill opacity=0.2]{-x+3}\closedcycle;}
        \only<+->{\addplot[black, fill=red, fill opacity=0.2]{x}\closedcycle;}
    \end{axis} 
\end{tikzpicture}%
\end{frame}

\begin{frame}{Attempt 3}
\begin{tikzpicture} 
    \begin{axis}
        \addplot[visible on=<+->, black, fill=red, fill opacity=0.2]{-x+3}\closedcycle;
        \addplot[visible on=<+->, black, fill=red, fill opacity=0.2]{x}\closedcycle;
    \end{axis} 
\end{tikzpicture}%
\end{frame}

\end{document}
7

Your Attempt 3 works if you put visible on=<+-> after the fill opacity option. The reason is that, in your MWE, the definition of visible on sets opacity=0, but this is overridden by fill opacity=0.2, which comes later. That's why the fills are already there, while the border lines are not.

MWE

\documentclass{beamer}
\usepackage{pgfplots}
\tikzset{
    invisible/.style={opacity=0},
    visible on/.style={alt={#1{}{invisible}}},
    alt/.code args={<#1>#2#3}{%
      \alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}} % \pgfkeysalso doesn't change the path
    },
  }
\begin{document}
\begin{frame}{Attempt 3}
\begin{tikzpicture} 
    \begin{axis}
        \addplot[black, fill=red, fill opacity=0.2,visible on=<+->]{-x+3}\closedcycle;
        \addplot[black, fill=red, fill opacity=0.2,visible on=<+->]{x}\closedcycle;
    \end{axis} 
\end{tikzpicture}%
\end{frame}
\end{document}

Output

enter image description here

  • If the figure has legends that correspond to each curve all legends appear simultaneously in the begining – aaragon Jun 8 '17 at 11:17
3

Here's one possibility using your approach with \only and controlling domain and ymin for both plots:

\documentclass{beamer}
\usepackage{pgfplots}

\begin{document}

\begin{frame}{Attempt 1}
\begin{tikzpicture} 
    \begin{axis}[ymin=-6]
        \only<+->{\addplot[black, fill=red, fill opacity=0.2,domain=-5:5]{-x+3}\closedcycle;}
        \only<+->{\addplot[black, fill=red, fill opacity=0.2,domain=-5:5]{x}\closedcycle;}
    \end{axis} 
\end{tikzpicture}%
\end{frame}

\end{document}

The result:

enter image description here

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.