0

Here is my code:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=1.14, label style={font=\huge}, tick label style={font=\huge}}
\usepgfplotslibrary{fillbetween}
\begin{document}

\pgfmathdeclarefunction{t_dist}{1}{% \pgfmathparse{gamma((#1 + 1)/2)/(\sqrt(#1pi)gamma(#1/2))*(1 + (x^2/#1))^(-(#1+1)/2)}% }

\begin{tikzpicture} \begin{axis}[ no markers, domain=-3.5:3.5, samples=300, axis x line=bottom, axis y line=left, every axis y label/.style={at=(current axis.above origin),anchor=south}, every axis x label/.style={at=(current axis.right of origin),anchor=west}, height=5cm, width=14cm, xtick={-3.5, -1.5, 0, 3.5}, ytick=\empty, enlargelimits=false, clip=false, axis on top, xticklabels={-$\infty$, -3, 0, $\infty$}, % so do it manually instead ]

\addplot [very thick,blue, name path=f] {t_dist(2)}; \path[name path=axis](axis cs:--3.5,0) -- (axis cs:-3,0); \addplot[thick, color=blue, fill=blue, fill opacity=0.5] fill between [of=f and axis, soft clip={domain=-3.5:-1.5},];

\end{axis}

\end{tikzpicture} \end{document}

It works just fine when I use a normal distribution, but for the t distribution I'm getting an error on line 23 that says "undefined control sequence".

Here's the working code with the normal distribution:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=1.14, label style={font=\huge}, tick label style={font=\huge}}
\usepgfplotslibrary{fillbetween}
\begin{document}

\pgfmathdeclarefunction{gauss}{2}{% \pgfmathparse{1/(#2sqrt(2pi))exp(-((x-#1)^2)/(2#2^2))}% }

\begin{tikzpicture} \begin{axis}[ no markers, domain=-3.5:3.5, samples=300, axis x line=bottom, axis y line=left, every axis y label/.style={at=(current axis.above origin),anchor=south}, every axis x label/.style={at=(current axis.right of origin),anchor=west}, height=5cm, width=14cm, xtick={-3.5, -1.5, 0, 3.5}, ytick=\empty, enlargelimits=false, clip=false, axis on top, xticklabels={-$\infty$, -3, 0, $\infty$}, ]

\addplot [very thick,blue, name path=f] {gauss(0,1)}; \path[name path=axis](axis cs:--3.5,0) -- (axis cs:-3,0); \addplot[thick, color=blue, fill=blue, fill opacity=0.5] fill between [of=f and axis, soft clip={domain=-3.5:-1.5},];

\end{axis}

\end{tikzpicture} \end{document}

How can I plot a t distribution on 2 degrees of freedom using tikz?

  • 1
    Does https://tex.stackexchange.com/a/120455/ help? – Torbjørn T. Sep 20 '20 at 16:52
  • Thanks @TorbjørnT. but I've already seen it. I checked whether "gamma" was understood by pgfmath, and it seems that it is (according to: https://www.latex4technics.com/?note=1ubx), so that's not the issue. – StatsSorceress Sep 20 '20 at 17:23
  • What is the definition of gamma distribution? –  Sep 20 '20 at 17:32
  • 1
    I don't really understand your comment, my point was that the answer I linked to shows you how to plot a student's t-distribution, so can't you use that code as a starting point to make your plot? pgfmath does not provide a gamma function, but Jake defines an approximation in that answer. – Torbjørn T. Sep 20 '20 at 17:41
  • @StatsSorceress: My TeX 2019 says: Package PGF Math Error: Unknown function 'gamma' –  Sep 20 '20 at 17:42
  • Thanks folks, especially @TorbjørnT. I had thought pgfmath had the gamma function based on the link that I sent, but I guess not. I'm using the post you linked to. Strange, my code still didn't work when I defined the gamma function. I'll just have to modify the code from that post. – StatsSorceress Sep 20 '20 at 18:03
  • If you want t_dist just for integer values it is OK and you can use factorial function instead of gamma that are equal for integers. –  Sep 20 '20 at 18:07
  • 1
    Note also that \sqrt should be sqrt (without backslash) in your t_dist function. –  Sep 20 '20 at 18:08

0 Answers0