# plot and annotate chi square distribution

My question is replacing Gaussian distribution with chi square distribution in following codes. So it should show a chi-square distribution instead of a normal distribution. Thanks in advance.

\documentclass{article}
\usepackage{pgfplots}
\begin{document}

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

\begin{tikzpicture}
\begin{axis}[
no markers, domain=0:8, samples=100,
axis y line = none,
axis x line* = bottom,
every axis x label/.style={at=(current axis.right of origin),anchor=west},
height=5cm, width=12cm,
xtick={2.5,5.5}, xticklabels = {$$,$$}, ytick=\empty,
enlargelimits=false, clip=false, axis on top,
grid = major
]
\addplot [fill=cyan!20, draw=none, domain=0:2.5] {gauss(4,1)} \closedcycle;
\addplot [fill=cyan!20, draw=none, domain=5.5:8] {gauss(4,1)} \closedcycle;

\draw [yshift=-0.3cm, latex-latex](axis cs:4,0) -- node [fill=white] {$0.35$}
(axis cs:4,0);

\draw [yshift=+2cm, latex-latex](axis cs:2.5,0) -- node [fill=white] {$H_0$
Do not Reject} (axis cs:5.5,0);

\draw [yshift=+2cm, latex-latex](axis cs:0,0) -- node [fill=white] {$H_0$
Reject} (axis cs:2.5,0);

\draw [yshift=+2cm, latex-latex](axis cs:5.5,0) -- node [fill=white] {$H_0$
Reject} (axis cs:8,0);
\end{axis}

\end{tikzpicture}
\end{document}

• No it is not a duplication. I cannot change gauss function in codes with a new function which can be called as chisquare. Because chi square has gamma function and I am not so good at functions in latex. any help will be appreciated Jan 6 '18 at 13:15
• I still think this is a duplicate. You seem to be right that the Gamma function cannot be easily written in LaTeX, that is why the first given solution in the aforementioned question shows a solution to this problem, when you have gnuplot installed, and the third solution shows an approximated solution using pure TikZ. Jan 6 '18 at 13:22
• Stefan thanks for your comment. However I still need help. I even cannot change the loop in the aforementioned question to a single distribution plot. Jan 6 '18 at 13:35
• Then I recommend asking a follow-up questions. Include a link to the answer from where you "copied" the code from and state what you have tried so far. Then chances are high, that someone will help you. (Since your question now will be about pgfplots, please add this as a tag.) Jan 6 '18 at 13:50
• @StefanPinnow Well this is a follow up what else is needed? Jan 6 '18 at 14:09

Not pretty code, but it works. The chi-square plotting is from cjorssen's answer to Plotting the chi square distribution with TikZ.

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
restrict y to domain = 0:0.5,
xtick=\empty,ytick=\empty,
axis lines*=left,
hide y axis,
clip=false,
width=8cm,
height=4cm
]

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,4)};

\path [name path=xax] (\pgfkeysvalueof{/pgfplots/xmin}, \pgfkeysvalueof{/pgfplots/ymin})
-- (\pgfkeysvalueof{/pgfplots/xmax}, \pgfkeysvalueof{/pgfplots/ymin});

\addplot [fill=blue!30] fill between[of=chi and xax, soft clip={domain=10:15}];

\draw (10, \pgfkeysvalueof{/pgfplots/ymax}) -- (10, \pgfkeysvalueof{/pgfplots/ymin}-0.05)
node[below,font=\footnotesize] {$\chi^2=2.547$};

\draw [stealth-] (13.5, \pgfkeysvalueof{/pgfplots/ymin}) -- (13.5, \pgfkeysvalueof{/pgfplots/ymin}-0.1)
node[below,font=\footnotesize] {Calculated $\chi^2=5.147$};

\draw [stealth-, shorten >=2pt] (rel axis cs:0,0.5) -- node[fill=white,font=\scriptsize]{Not rejecting of $H_0$} (10,0 |- {rel axis cs:0,0.5});

\draw [-stealth, shorten <=2pt] (10,0 |- {rel axis cs:0,0.5}) -- node[fill=white,align=center,font=\scriptsize]{Rejecting\\region\\of $H_0$} (rel axis cs:1,0.5);

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

• Thanks. It is pretty. However I got an error ! Package pgfplots Error: Sorry, 'compat=1.15' is unknown in this context. Plea se use at most 'compat=1.14'. Jan 6 '18 at 19:10
• Wouldn't the answer be fitted better at "the other question"? Jan 6 '18 at 19:11
• @MehmetOZC So do what the error says, and change compat=1.15 to compat=1.14. I have version 1.15 of pgfplots, you seem to have version 1.14. Jan 6 '18 at 19:22
• @StefanPinnow Maybe, maybe not. Jan 6 '18 at 19:25

Run with xelatex

\documentclass[pstricks]{standalone}
\usepackage{pst-func}
\begin{document}

\psset{xunit=1.2cm,yunit=10cm,plotpoints=200}
\begin{pspicture*}(-0.75,-0.1)(10,0.65)
\multido{\rnue=0.5+0.5,\iblue=0+10}{10}{%
\psChiIIDist[linewidth=1pt,linecolor=blue!\iblue,nue=\rnue]{0.01}{9}}
\psaxes[Dy=0.1]{->}(0,0)(9.5,.6)
\end{pspicture*}

\end{document}


• Herbert thanks for your answer. It really produces chi square. It is in your previous post and I have work on it. However it is a pspicture. In my project I work with tikzpicture. Is it possible to write codes in tikzpicture instead of pspicture? Also I need a single distribution. In your code, it produces 10 distribution in the same figure. Jan 6 '18 at 14:48
• I always use PSTricks. Instead of \multido use only one \psChiIIDist. Then you'll get only one distribution
– user2478
Jan 6 '18 at 15:16