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.



 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;
\addplot [very thick,cyan!50!black] {gauss(4,1)};

\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);

  • 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 – Mehmet OZC 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. – Stefan Pinnow 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. – Mehmet OZC 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.) – Stefan Pinnow Jan 6 '18 at 13:50
  • @StefanPinnow Well this is a follow up what else is needed? – percusse 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.

output of code

    restrict y to domain = 0:0.5,
    axis lines*=left,
    hide y axis,

   \addplot+[name path=chi,mark={}] gnuplot[raw gnuplot] {%
        isint(x) = (int(x)==x);
        log2 = 0.693147180559945;
        set xrange [1.00000e-5:15.0000];
        set yrange [0.00000:0.500000];
        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);

  • 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'. – Mehmet OZC Jan 6 '18 at 19:10
  • Wouldn't the answer be fitted better at "the other question"? – Stefan Pinnow 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. – Torbjørn T. Jan 6 '18 at 19:22
  • @StefanPinnow Maybe, maybe not. – Torbjørn T. Jan 6 '18 at 19:25

Run with xelatex




enter image description here

  • 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. – Mehmet OZC 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

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.