[Tex/LaTex] Plotting the chi square distribution with TikZ

tikz-pgf

I have tried without success to plot the curve of the chi-squared distribution.
Is there a generous soul who can come to my rescue.

Best Answer

If you can access gnuplot, you can try this. This is an adapted version of a gnuplot demo file.

enter image description here

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[%
    xlabel = $x$,
    ylabel = {Probability density},
    samples = 200,
    restrict y to domain = 0:0.5,
    domain = 0.01:15]
    \foreach \k in {1,...,8} {%
      \addplot+[mark={}] gnuplot[raw gnuplot] {%
        isint(x) = (int(x)==x);
        log2 = 0.693147180559945;
        chisq(x,k)=k<=0||!isint(k)?1/0:x<=0?0.0:exp((0.5*k-1.0)*log(x)-0.5*x-lgamma(0.5*k)-k*0.5*log2);
        set xrange [1.00000e-5:15.0000];
        set yrange [0.00000:0.500000];
        samples=200;
        plot chisq(x,\k)};
    \addlegendentryexpanded{$k = \k$}}
  \end{axis}
\end{tikzpicture}
\end{document}
Related Question