# Fill area under Student's-t distribution with pgfplot-gnuplot

I'd like to draw the pdf of a Student's-t distribution with given degree of freedom and fill the area under the curve on a given interval.

The solution I found for Student's-t distribution curve uses gnuplot, which somehow makes it harder (for me) to find a way to fill the area. I created the curve the following way:

\documentclass{standalone}
\usepackage{pgfplots}

\def\basefunc{
gamma((\n+1)/2.)/(sqrt(\n*pi)*gamma(\n/2.))*((1+(x*x)/\n)^(-(\n+1)/2.))
}
\def\n{7}

\begin{document}
\begin{tikzpicture}
\begin{axis}[samples=200,ymin=0,xmin=-6,xmax=6]
\addplot gnuplot [black,thick,smooth,no marks,domain={-6:+6}]{\basefunc};
\end{axis}
\end{tikzpicture}
\end{document}


And I need something like this (the most important thing is the shading, labeling is not necessary):

I appreciate any idea how to fill it similarly.

• @CarLaTeX I've had no problems. Do you have gnuplot installed on your system? Sep 21 '17 at 10:13
• @Ignasi Sorry, I don't. Sep 21 '17 at 10:29

## 2 Answers

Alternatively plot three times with different domains, using \closedcycle for the plots with filling.

\documentclass{standalone}
\usepackage{pgfplots}

\def\basefunc{
gamma((\n+1)/2.)/(sqrt(\n*pi)*gamma(\n/2.))*((1+(x*x)/\n)^(-(\n+1)/2.))
}
\def\n{7}

\begin{document}
\begin{tikzpicture}
\begin{axis}[samples=200,ymin=0,xmin=-6,xmax=6]
\addplot gnuplot [no marks,fill=blue,domain={-6:-2}]{\basefunc} \closedcycle;
\addplot gnuplot [no marks,fill=red,domain={2:6}]{\basefunc} \closedcycle;
\addplot gnuplot [black,thick,smooth,no marks,domain={-6:6}]{\basefunc};
\end{axis}
\end{tikzpicture}
\end{document}


• Thank you, I prefer this solution. However if I compile it, gnuplot only creates the table for the last function and finally I get the whole functin filled with red. I compile with pdflatex -shell-escape <file.tex> then gnuplot <file.pgf-plot.gnuplot > and again with pdflatex. What is wrong with the way I compile?
– grus
Sep 22 '17 at 10:34
• @grus Only the first of those steps is required. With pdflatex --shell-escape file.tex, gnuplot is called in the background to generate the data, which is then read in by pgfplots. If I do that one run of pdflatex I get the output shown in my answer. Sep 22 '17 at 10:46

You can use fillbetween pgfplots library (section 5.7 in pgfplots documentation).

As you want to fill two disjoint areas, will need two commands, one for each area.

\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}

\def\basefunc{
gamma((\n+1)/2.)/(sqrt(\n*pi)*gamma(\n/2.))*((1+(x*x)/\n)^(-(\n+1)/2.))
}
\def\n{7}

\begin{document}
\begin{tikzpicture}
\begin{axis}[samples=200,ymin=0,xmin=-6,xmax=6]
\addplot[name path=A] gnuplot [black,thick,smooth,no marks,domain={-6:+6}]{\basefunc};
\path [name path=axis]
(rel axis cs:0,0) --
(rel axis cs:1,0);

\addplot[orange] fill between [of=A and axis, soft clip={domain=-6:-2}];

\addplot[pink] fill between [of=A and axis, soft clip={domain=2:6}];

\end{axis}
\end{tikzpicture}
\end{document}


• Thank you! However my pgfplot it seems does not include the fillbetween library :( I am trying to figure out how to make that work and come back later.
– grus
Sep 22 '17 at 10:44
• @grus If pgfplots doesn't have fillbetween library, you need to update your tex system (at least pgfplots). Sep 22 '17 at 12:03
• Finally I could update pgfplots (it was not obvious, took awhile to make it work), the code works now. :) Thank you!
– grus
Sep 24 '17 at 18:43