Example: Animated distributions

Published 2007-12-17 | Author: Brian S. Marks

The animate.sty package is a useful tool for creating Javascript driven animations in PDF files. In this example it is used to animate probability distributions with varying parameters. GNUPLOT is used to compute the distribution curves. Download the PDF to see the animation.

Update: Example recompiled using animate.sty version 2008-05-16. Animation should now render correctly in all versions of Acrobat Reader v >= 6.0.


Acrobat Reader v >= 6.0 is required to view this example.

This example takes some time to compile. A large number of auxiliary files will be created.

Author:Brian S. Marks
Source:STAT-S300 — Introduction to Applied Statistical Methods notes

Download as: [PDF] [TEX]  •  [Open in Overleaf]

Animated distributions

Do you have a question regarding this example, TikZ or LaTeX in general? Just ask in the LaTeX Forum.
Oder frag auf Deutsch auf TeXwelt.de. En français: TeXnique.fr.

% Animated distributions
% Author: Brian S. Marks <http://mypage.iu.edu/~bsmarks/>
% Note that shell-escape has to be enabled to generate the plots.
% The animations can only be viewed in Acrobat (Reader) v >= 6.

% Define a the counter cnt. Used to identify files generated for use
% with Gnuplot.

% Macro for drawing one frame of the F-distribution animation.
    % shade the critical region tail
    \draw[fill,orange]  (#1,0) -- plot[id=5\thecnt,domain=#1:5.5,samples=50]
        function {#4*(x**(0.5*#2-1))*((1+#2*x/#3)**(-0.5*#2-0.5*#3))}
            -- (5.5,0) -- cycle;

    % draw the F distribution curve
        function {#4*(x**(0.5*#2-1))*((1+#2*x/#3)**(-0.5*#2-0.5*#3))};

    % draw the F axis
    \draw[->] (0,0) -- (6,0) node[right] {$F$};
    % label the critical region boundary
    \draw (#1,0) -- (#1,-0.02) node[below] {$#1$};
    % label 0
    \draw (0,0) -- (0,-0.02) node[below] {$0$};

    % add some lables for degrees of freedom and alpha level
    \draw (2,0.5) node[right] {$df_1 = #2$};
    \draw (2,0.4) node[right] {$df_2 = #3$};
    \draw (2,0.3) node[right] {$\alpha = 0.10$};

    % draw the y axis
    \draw[very thin,->] (0,0) -- (0,0.8);

    % First draw the upper distribution.
    % Shade the critical region:
    \fill[red!30] (0.658,0)  -- plot[id=f3,domain=0.658:3,samples=50]
        function {exp(-x*x*0.5/0.16)} -- (3,0) -- cycle;

    % Draw the normal distribution curve
    \draw[blue!50!black,smooth,thick] plot[id=f1,domain=-2:3,samples=50]
    function {exp(-x*x*0.5/0.16)};
    % Draw the x-axis
    \draw[->,black] (-2.2,0) -- (3.2,0);
    % Put some ticks and tick labels in:
    \foreach \x in {-2,-1,0,1,2,3}
    \draw (\x,0) -- (\x,-0.1) node[below] {$\x$};
    % Put in a label for the critical region boundary:
    \draw[red!50!black,thick] (0.658,0) node[below,yshift=-0.5cm] {0.658}
    -- (0.658,0.85);

    % Put in labels for accepting or rejecting the null hypothesis with
    % the corresponding regions:
    \draw[red!50!black,thick,->] (0.688,0.7) -- (1.3,0.7)
        node[anchor=south] {Reject  $H_0$};
    \draw[red!50!black,thick,->] (0.628,0.7) -- (-1,0.7)
        node[anchor=south]{\parbox{1.5cm}{\raggedright Fail to reject $H_0$}};

    % Add a label to the upper picture, when the null is true
    \draw (-3,1) node[above,draw,fill=green!30] {$H_0$ is true:};

    % Label the critical region with an alpha level:
    \draw[<-,thick] (0.75,0.05) -- (1.6,0.2) node[right,yshift=0.3cm]
    {\begin{tabular}{l} $\alpha=0.05$ \\ (Type I error rate) \end{tabular}};

    % Add a label showing the effect size between the two plots:
    \draw[very thin] (0,-1) -- (0,-0.5);
    \draw[<->,thick] (0,-1) node[left] {Effect size:  #1} -- (#1,-1);
    \draw[thick] (0,-.9) -- (0,-1.1);

    \draw[very thin] (#1,-1) -- (#1,-1.7);
    \draw[thick] (#1,-.9) -- (#1,-1.1);

    % Now draw the lower distribution showing the effect size:
    % Shade the "reject H0" region red
    \fill[red!30] (0.658,0)  -- plot[id=f3\thecnt,domain=0.658:3,samples=50]
        function {exp(-(x-#1)*(x-#1)*0.5/0.16)} --
        (3,0) -- cycle;
        % Shade the "accept H0" region blue
    \fill[blue!30] (-2,0) -- plot[id=f4\thecnt,domain=-2:0.658,samples=50]
        function {exp(-(x-#1)*(x-#1)*0.5/0.16)} --
        (0.658,0) -- cycle;

        % Draw the shifted normal distribution:
    \draw[blue!50!black,smooth,thick] plot[id=f1\thecnt,domain=-2:3,samples=50]
            function {exp(-(x-#1)*(x-#1)*0.5/0.16)};

        % Draw the x-axis and put in some ticks and tick labels
    \draw[->,black] (-2.2,0) -- (3.2,0);
    \foreach \x in {-2,-1,0,1,2,3}
            \draw (\x,0) -- (\x,-0.1) node[below] {$\x$};

        % Draw and label the critical region boundary
    \draw[red!50!black,very thick] (0.658,0) node[below,yshift=-0.5cm] {0.658}
        -- (0.658,1.0);
    \draw[red!50!black,very thick,->] (0.688,0.7) -- (2.7,0.7)
        node[anchor=south west] {Reject  $H_0$};
    \draw[red!50!black,very thick,->] (0.628,0.7) -- (-0.5,0.7)
        node[anchor=south]{\parbox{1.5cm}{\raggedright Fail to reject $H_0$}};

    % Add a label to the lower picture, when the alternative hypothesis is true:
    \draw (-3,1) node[above,draw,fill=green!30] {$H_a$ is true:};

        % Add labels showing the statistical power and the Type II error rate:
    \draw[<-,thick] (1.5,0.1) -- (3,0.2) node[anchor=south west]
        {Power = \large #2};
    \draw[<-,thick] (0.4,0.1) -- (-1,0.2) node[left,yshift=0.3cm]
        $\beta$ = {\large #3} \\ (Type II error rate) \end{tabular}};


  General form for the $F$ distribution with $\nu_1$ and $\nu_2$ degrees of
    f(F; \nu_1, \nu_2) = \frac{\Gamma\left(\frac{\nu_1+\nu_2}{2}\right)}
    \frac{F^{\frac{\nu_1-2}{2}}}{\left[1 +

  Critical region for an $F$ test for various degrees of freedom:




  Statistical power in hypothesis testing:




  • #1 Victor, August 6, 2009 at 4:53 a.m.

    Hola: Trabajo con Miktex y windows y lo ejuta el tex pero no se ve la lineas animadas solo los numeros. Favor ayuda estoy preparando una presentacion con curva normal.


  • #2 davebv, October 27, 2009 at 10:55 a.m.

    Hi, I am in the same situation, I just downloaded the .tex but do not see the plots / graphs, just the numbers ans lines.

    May you post the steps to reproduce the pdf you have done?

    thanks a lot for the example

  • #3 Kjell Magne Fauske, October 27, 2009 at 1:08 p.m.

    To compile the example Gnuplot has to be available, and you also have to enable shell escape so that PGF can run gnuplot automatically. The process is described in the gnuplot basics example. If gnuplot is available you should be able to compile the example using:

    > pdflatex --shell-escape animated-distributions.tex
  • #4 therimalaya, July 10, 2012 at 10:52 a.m.

    I'm also having the same problem. How to do the things you've described above in Windows. I'm using MikTex in windows 7.

  • #5 Brian S. Marks, August 9, 2012 at 6:50 p.m.

    I created the above files originally on a machine running Windows XP, actually. I don't use MikTeX, though, so I apologize that I cannot answer your question directly. I feel certain that there is a way to make it work with MikTeX and gnuplot for Windows, though.

    I use teTeX under Cygwin for Windows. I've installed pgf/TikZ, animate.sty, and, as Kjell Magne said, I installed gnuplot, all under Cygwin. Then use the command he provides (with --shell-escape) above to compile it.

    I hope that helps.

  • #6 kalıcı makyaj, May 6, 2013 at 7:32 p.m.

    Thanks for sharing such a pleasant thinking, post is good, thats why i have read it entirely

Adding comments is currently not enabled.

Scientific and technical areas


LaTeX Beginners Guide

Limited discount 50%
coupon code tDRet6Y

Creative Commons License