使用 tikz 包绘制分布

使用 tikz 包绘制分布

我正在尝试绘制一个受到审查Gamma 分布。我已成功绘制了 Gamma-pdf 图并引用了审查点,但我无法抑制超出该点的任何内容。换句话说,在审查点 n-star 之后应该没有分布图。

我的策略是尝试用白色填充 n-star 以外的分布图。我把填充边框弄粗了,虽然这有帮助,但并没有完成工作(您仍然可以看到 n-star 以外的细线)。我附上了生成的图的图像并粘贴了代码。任何可以解决这个问题的想法我都会很感激。非常感谢!

\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}

在此处输入图片描述

答案1

要限制用于评估函数的域,请设置domain=0:6。如果您希望 x 轴延伸到域之外,请设置xmax=10(例如):

\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}

答案2

运行latex->dvips->ps2pdfxelatex

\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}

在此处输入图片描述

相关内容