# [Tex/LaTex] Student t-distribution with TikZ

tikz-pgf

I'd like to draw a Student's t-distribution with five degrees of freedom using TikZ, then another with 10 degrees of freedom, etc.

In the program I am working in the degrees of freedom will be a random number from 1 to 20, so I need a t-distribution for the degrees of freedom assigned by Perl randomization.

This is a pgfplots/gnuplot solution.

For \addplot gnuplot {…} to work you need to have a working installation of gnuplot on your machine and have to call pdflatex with write18 enabled (i.e. --shell-escape or --enable-write18).
How pgfplots and gnuplot interact can be studied in the pgfplots manual in subsection 4.2.5 “Computing Coordinates With Mathematical Expressions (gnuplot)”.

There are two foreach loops in this code. One that loops over tikzpicture and gives you one plot per picture, the other one loops over \addplot so that you will get one picture with nineteen plots.

Edit: Apparently gnuplot sees /2 as an integer rather than a floating point division.
The function is therefore:

gamma((\n+1)/2.)/(sqrt(\n*pi)*gamma(\n/2.))*((1+(x*x)/\n)^(-(\n+1)/2.))% or
gamma((\n+1)/2.)/(sqrt(\n*pi)*gamma(\n/2.))/((1+(x*x)/\n)^((\n+1)/2.))% or
gamma(.5*(\n+1))/(sqrt(\n*pi)*gamma(.5*\n))*((1+(x*x)/\n)^(-.5*(\n+1)))% or
gamma(.5*(\n+1))/(sqrt(\n*pi)*gamma(.5*\n))/((1+(x*x)/\n)^(.5*(\n+1)))%


## Code

\documentclass[tikz,border=2pt]{standalone}
\usepackage{pgfplots}
\def\basefunc{%
gamma(.5*(\n+1))/(sqrt(\n*pi)*gamma(.5*\n))*((1+x^2/\n)^(-.5*(\n+1)))%
}
\begin{document}
\foreach \n in {1,...,20}{
\begin{tikzpicture}
\begin{axis}[
ymin=0,
ymax=.41,
]
smooth,
no marks,
domain={-6:+6},
]{\basefunc};
\legend{$n = \n$}
\end{axis}
\end{tikzpicture}
}
\begin{tikzpicture}
\begin{axis}[
ymin=0,
ymax=.41,
]
\foreach \n in {2,...,20}{
very thin,
smooth,
no marks,
domain={-6:+6},
]{\basefunc};
}
\end{axis}
\end{tikzpicture}
\end{document}


## Animated output of the first tikzpictures ## Output of the second tikzpicture 