3

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):

Student's t-distribution with two tails

I appreciate any idea how to fill it similarly.

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

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}

output of code

2
  • 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
  • 2
    @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
3

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}

enter image description here

3
  • 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
  • 1
    @grus If pgfplots doesn't have fillbetween library, you need to update your tex system (at least pgfplots).
    – Ignasi
    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

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.