轴 pgfplots 二项式定律

轴 pgfplots 二项式定律

我有一个问题:我想要二项式定律在正确的位置。

\begin{tikzpicture}[
    declare function={binom(\k,\n,\p)=\n!/(\k!*(\n-\k)!)*\p^\k*(1-\p)^(\n-\k);},
    declare function={poisson(\k,\l)=((exp(-\l)*\l^\k))/(\k!);},    
    declare function={mu=100;}, %
    declare function={sigma=7.071;}, %
    declare function={normal(\m,\s)=1/(\s*sqrt(2*pi))*exp(-(x-\m)^2/(2*\s^2));}
]

\begin{axis}[
    samples at={0,...,10},
    axis x line=bottom,axis y line = left,
    yticklabel style={
        /pgf/number format/fixed,
        /pgf/number format/fixed zerofill,
        /pgf/number format/precision=2
    },
    ybar=0pt, bar width=.1  ,
    xlabel = {$k$} ,
    ylabel = {$proba$ } ,
    legend style={
    at={(0,0)},
    anchor=north east,at={(axis description cs:0,-0.1)}}
]
\addplot [fill=red, fill opacity=0.5, bar width=1] {binom(x,100,0.02)};
\addplot [fill=cyan, fill opacity=1] {poisson(x,2)};
%\addplot [domain=40:120,samples=201,thin, smooth, color=red] {normal(mu,sigma)};
%\addplot [fill=orange, fill opacity=0.5] {binom(x,40,0.5)}; \addlegendentry{$p=0.5$}
\end{axis}
\end{tikzpicture}

我尝试过,但enlargelimits=true,效果不好 :/

有任何想法吗 ?

最好的,

答案1

尝试enlargelimits=true用替换enlarge x limits=<value>

您的直方图看起来怎么样并不完全清楚。像这样吗?

在此处输入图片描述

\documentclass[border=3.14192]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
\begin{tikzpicture}[
    declare function={binom(\k,\n,\p)=\n!/(\k!*(\n-\k)!)*\p^\k*(1-\p)^(\n-\k);},
    declare function={poisson(\k,\l)=((exp(-\l)*\l^\k))/(\k!);},
    declare function={mu=100;}, %
    declare function={sigma=7.071;}, %
    declare function={normal(\m,\s)=1/(\s*sqrt(2*pi))*exp(-(x-\m)^2/(2*\s^2));}
]

\begin{axis}[
    samples at={0,...,10},
    axis lines = left,
    enlarge x limits=0.25,
    yticklabel style={
        /pgf/number format/.cd,fixed,zerofill,precision=2
                    },
    ybar=7mm, 
    bar width=4.mm,
    xlabel = {$k$},
    ylabel = {proba},
    legend pos=north east
]
\addplot [fill=red,] {binom(x,100,0.02)};
\addplot [fill=cyan, fill opacity=0.5]   {poisson(x,2)};
\addlegendentry{$p=0.5$}
\end{axis}
\end{tikzpicture}
\end{document}
\end{scope}

或这个:

在此处输入图片描述

轴前导码如下:

\begin{axis}[x=11mm,
    samples at={0,...,10},
    axis lines = left,
    enlarge x limits=0.1,
    yticklabel style={
/pgf/number format/.cd,fixed,zerofill,precision=2},
    ybar, 
    bar width=4.4 mm,
    xlabel = {$k$} ,
    ylabel = {proba} ,
    legend pos=north east
]

附录: 以上两种解决方案均基于您的 MWE。但现在看来,您正在寻找以下图表:

在此处输入图片描述

制作方:

\documentclass[border=3.14192]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
    \begin{tikzpicture}[
declare function={binom(\k,\n,\p)=\n!/(\k!*(\n-\k)!)*\p^\k*(1-\p)^(\n-\k);},
declare function={poisson(\k,\l)=((exp(-\l)*\l^\k))/(\k!);},
                        ]
\begin{axis}[
    samples at={0,...,10},
    axis lines = left,
    enlarge x limits=0.05,
    yticklabel style={
        /pgf/number format/.cd,fixed,zerofill,precision=2
                    },
    ybar interval=0.7,
    x tick label as interval,
    xlabel = {$k$},
    ylabel = {proba},
    legend pos=north east
]
\addplot [fill=red ] {binom(x,100,0.02)};
\addplot [fill=cyan] {poisson(x,2)};
\end{axis}
    \end{tikzpicture}
\end{document}

相关内容