7

I'm trying to generate a plot of a censored Gamma distribution. I've managed to make the plot of the Gamma-pdf and reference the censoring point, but I'm failing to suppress anything beyond it. In other words, after the censoring point n-star there should be no distribution diagram.

My strategy was to try to fill the distribution's plot beyond n-star in white. I made the filling border thick and while it helped it didn't get the job done (you can still see a thin line beyond n-star). I'm attaching an image of the generated plot and also pasting the code. I would appreciate any ideas that could solve this problem. Many thanks!

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}[
    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);}
]

\begin{axis}[
  no markers, domain=0:9, samples=100,
  axis lines=left, xlabel=$n_t^i$, ylabel=$f_n(.)$,
  every axis y label/.style={at=(current axis.above origin),anchor=east},
  every axis x label/.style={at=(current axis.right of origin),anchor=north},
  height=5cm, width=9cm,
  xtick={6.0}, ytick=\empty,
  xticklabels={$n^*$},
  %xticklabels={$\bar n (\theta_t)$},
  enlargelimits=false, clip=false, axis on top,
  grid = major
  ]

\addplot [very thick,cyan!20!black,domain=0:20] {gammapdf(x,2,2)};
\addplot [fill=cyan!20, draw=none, domain=0:6.0] {gammapdf(x,2,2)} \closedcycle;
\addplot [very thick, fill=white!20!white, draw=none, domain=6.01:20] {gammapdf(x,2,2)} \closedcycle;


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

enter image description here

percusse
  • 157,807
Colenso
  • 153

2 Answers2

10

To limit the domain used for evaluating the function, set domain=0:6. If you want the x axis to extend further than the domain, set xmax=10 (for example):

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}[
    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);}
]

\begin{axis}[
  no markers, domain=0:6, samples=100,
  axis lines=left, xlabel=$n_t^i$, ylabel=$f_n(.)$,
  every axis y label/.style={at=(current axis.above origin),anchor=east},
  every axis x label/.style={at=(current axis.right of origin),anchor=north},
  height=5cm, width=9cm,
  xtick={6.0}, ytick=\empty,
  xticklabels={$n^*$},
  enlargelimits=false, clip=false, axis on top,
  grid = major,
  xmax=10
  ]

\addplot [very thick,cyan!20!black] {gammapdf(x,2,2)};
\addplot [fill=cyan!20, draw=none] {gammapdf(x,2,2)} \closedcycle;


\end{axis}
\end{tikzpicture}
\end{document}
Jake
  • 232,450
4

Run with latex->dvips->ps2pdf or with xelatex

\documentclass{article}
\usepackage{pst-func}  
\begin{document}

\psset{xunit=1.2cm,yunit=10cm,plotpoints=200}
\begin{pspicture}(-0.75,-0.06)(6.6,0.45)
\pscustom[fillstyle=solid,fillcolor=blue!20,linestyle=none]{% fill area
  \psGammaDist[alpha=1.8,beta=0.9]{0.01}{3}
  \psline[](3,0)(0,0)
}
\psGammaDist[alpha=1.8,beta=0.9,linewidth=1.2pt,linecolor=blue]{0.01}{3}
\psline[linecolor=black!10,linewidth=1pt](3,0)(3,0.4)
\psaxes[labels=none,ticks=none]{->}(0,0)(6.5,0.4)[$n_t^i$,-90][$f_n(.)$,180]
\psxTick(3){n^*}
\end{pspicture}

\end{document}

enter image description here