Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to draw gamma distribution with different shape and scale parameters. The following MWE displayed error. The error information is : enter image description here

Here is MWE:



axis x line=bottom,
axis y line=left
smooth] {\gammaPDF(9,0.5)};
share|improve this question
remove the backslash from gammaPDF and define gamma –  percusse Jun 21 '13 at 19:38
@percusse: From the link:tex.stackexchange.com/questions/80345/…, where the gamma function is used but no predefined. So why I can't use it directly here. –  user22986 Jun 21 '13 at 20:07
@user22986: In the answer you linked to, the function is evaluated using the gnuplot backend. The gamma function isn't defined in the PGF math parser. –  Jake Jun 21 '13 at 20:21

1 Answer 1

up vote 10 down vote accepted

Here's the gamma PDF using the gamma function approximation that Wolfram Alpha spits out:


    declare function={gamma(\z)=
    2.506628274631*sqrt(1/\z)+ 0.20888568*(1/\z)^(1.5)+ 0.00870357*(1/\z)^(2.5)- (174.2106599*(1/\z)^(3.5))/25920- (715.6423511*(1/\z)^(4.5))/1244160)*exp((-ln(1/\z)-1)*\z;},
    declare function={gammapdf(\x,\k,\theta) = 1/(\theta^\k)*1/(gamma(\k))*\x^(\k-1)*exp(-\x/\theta);}

    axis lines=left,
    legend entries={$k=1\quad \theta=1$,$k=2\quad \theta=2$, $k=9\quad \theta=0.5$}
\addplot [smooth, domain=0:20] {gammapdf(x,1,2)};
\addplot [smooth, domain=0:20, red] {gammapdf(x,2,2)};
\addplot [smooth, domain=0:20, blue] {gammapdf(x,9,0.5)};
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.