如何在 tikz 中绘制三项分布?

如何在 tikz 中绘制三项分布?

我正在尝试绘制如下所示的 3d 概率质量函数(三项分布的概率直方图)。无法在网上找到任何有用的信息,用于给定函数(联合 pmf)进行如下所示的 3D 离散绘图。请帮忙。

在此处输入图片描述

功能:
在此处输入图片描述

图片来自这本书这里第 132 页。概率和统计推断,作者:Hoggs 等人。

更新:
谢谢@土拨鼠,我已设法将其提升到以下水平。但尚未完全理解他的代码。

在此处输入图片描述

答案1

这是一个提议(第一次编辑:已解决的dimension too large问题;第二次编辑:关注了@AndréC很好的建议稍微重新定义立方体图标记。重新定义立方体标记实际上可以摆脱一个丑陋的点:与我以前的回答,不再需要作弊,即手动调整z刻度。)。3D 条形图表面的颜色由 pgf 键cube top color等设置。

\documentclass[tikz,border=3.14pt]{standalone}
\usetikzlibrary{calc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\pgfkeys{/tikz/.cd,
cube top color/.store in=\CubeTopColor,
cube top color=blue!60,
cube front color/.store in=\CubeFrontColor,
cube front color=blue!30,
cube side color/.store in=\CubeSideColor,
cube side color=blue!40,
}
\makeatletter
 \pgfdeclareplotmark{my cube*}
                {%
                        \pgfplots@cube@gethalf@x
                        \let\pgfplots@cube@halfx=\pgfmathresult
                        \pgfplots@cube@gethalf@y
                        \let\pgfplots@cube@halfy=\pgfmathresult
                        \pgfplots@cube@gethalf@z
                        \let\pgfplots@cube@halfz=\pgfmathresult
                        \pgfmathparse{0*\pgfplots@cube@halfz}
                        \let\pgfplots@cube@topz=\pgfmathresult
                        %
                        \pgfplotsifaxissurfaceisforeground{0vv}{%
                                \pgfsetfillcolor{\CubeFrontColor}
                                \pgfpathmoveto{\pgfplotsqpointxyz{-\pgfplots@cube@halfx}{-\pgfplots@cube@halfy}{-\pgfplots@cube@halfz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{-\pgfplots@cube@halfx}{-\pgfplots@cube@halfy}{\pgfplots@cube@topz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{-\pgfplots@cube@halfx}{ \pgfplots@cube@halfy}{\pgfplots@cube@topz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{-\pgfplots@cube@halfx}{ \pgfplots@cube@halfy}{-\pgfplots@cube@halfz}}%
                                \pgfpathclose
                                \pgfusepathqfillstroke
                        }{% 
                                \pgfsetfillcolor{\CubeFrontColor}
                                \pgfpathmoveto{\pgfplotsqpointxyz{ \pgfplots@cube@halfx}{-\pgfplots@cube@halfy}{-\pgfplots@cube@halfz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{ \pgfplots@cube@halfx}{-\pgfplots@cube@halfy}{\pgfplots@cube@topz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{ \pgfplots@cube@halfx}{ \pgfplots@cube@halfy}{\pgfplots@cube@topz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{ \pgfplots@cube@halfx}{ \pgfplots@cube@halfy}{-\pgfplots@cube@halfz}}%
                                \pgfpathclose
                                \pgfusepathqfillstroke
                        }%
                        \pgfplotsifaxissurfaceisforeground{v0v}{%
                                \pgfsetfillcolor{\CubeSideColor}
                                \pgfpathmoveto{\pgfplotsqpointxyz{-\pgfplots@cube@halfx}{-\pgfplots@cube@halfy}{-\pgfplots@cube@halfz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{-\pgfplots@cube@halfx}{-\pgfplots@cube@halfy}{\pgfplots@cube@topz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{ \pgfplots@cube@halfx}{-\pgfplots@cube@halfy}{\pgfplots@cube@topz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{ \pgfplots@cube@halfx}{-\pgfplots@cube@halfy}{-\pgfplots@cube@halfz}}%
                                \pgfpathclose
                                \pgfusepathqfillstroke
                        }{% 
                                \pgfsetfillcolor{\CubeSideColor}
                                \pgfpathmoveto{\pgfplotsqpointxyz{-\pgfplots@cube@halfx}{ \pgfplots@cube@halfy}{-\pgfplots@cube@halfz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{-\pgfplots@cube@halfx}{ \pgfplots@cube@halfy}{\pgfplots@cube@topz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{ \pgfplots@cube@halfx}{ \pgfplots@cube@halfy}{\pgfplots@cube@topz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{ \pgfplots@cube@halfx}{ \pgfplots@cube@halfy}{-\pgfplots@cube@halfz}}%
                                \pgfpathclose
                                \pgfusepathqfillstroke
                        }%
                        \pgfplotsifaxissurfaceisforeground{vv0}{%
                                \pgfsetfillcolor{\CubeTopColor}
                                \pgfpathmoveto{\pgfplotsqpointxyz{-\pgfplots@cube@halfx}{-\pgfplots@cube@halfy}{-\pgfplots@cube@halfz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{-\pgfplots@cube@halfx}{ \pgfplots@cube@halfy}{-\pgfplots@cube@halfz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{ \pgfplots@cube@halfx}{ \pgfplots@cube@halfy}{-\pgfplots@cube@halfz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{ \pgfplots@cube@halfx}{-\pgfplots@cube@halfy}{-\pgfplots@cube@halfz}}%
                                \pgfpathclose
                                \pgfusepathqfillstroke
                        }{% 
                                \pgfsetfillcolor{\CubeTopColor}
                                \pgfpathmoveto{\pgfplotsqpointxyz{-\pgfplots@cube@halfx}{-\pgfplots@cube@halfy}{\pgfplots@cube@topz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{-\pgfplots@cube@halfx}{ \pgfplots@cube@halfy}{\pgfplots@cube@topz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{ \pgfplots@cube@halfx}{ \pgfplots@cube@halfy}{\pgfplots@cube@topz}}%
                                \pgfpathlineto{\pgfplotsqpointxyz{ \pgfplots@cube@halfx}{-\pgfplots@cube@halfy}{\pgfplots@cube@topz}}%
                                \pgfpathclose
                                \pgfusepathqfillstroke
                        }%
            }
\makeatother

\begin{document}
\pgfmathsetmacro{\gconv}{2*326.32446}
% from https://tex.stackexchange.com/a/435234/121799
\begin{tikzpicture}[declare function={% (0.00001*\n!/(0.01*\x!*0.01*\y!*0.01*(\n-\x-\y)!))*
f(\x,\y,\px,\py,\n)=(0.000001*\n!/(0.01*\x!*0.01*\y!*0.01*(\n-\x-\y)!))*pow(\px,\x)*pow(\py,\y)*pow(1-\px-\py,\n-\x-\y);}] % 
\pgfplotsset{set layers}
\begin{axis}[% from section 4.6.4 of the pgfplotsmanual
        view={40}{40},
        %x dir=reverse,
        %y dir=reverse,
        width=320pt,
        height=280pt,
        mesh,
        %mesh/ordering=x varies,
        z buffer=auto,%reverse xy seq,
        xmin=-0.5,xmax=5.5,
        ymin=-0.5,ymax=5.5,
        zmin=0,zmax=0.3,
        enlargelimits=upper,
        ztick={0,0.15,0.3},
        xtick=data,
        extra tick style={grid=major},
        ytick={0,...,5},xtick={0,...,5},
        grid=minor,
        xlabel={$x$},
        ylabel={$y$},
        zlabel={$f(x,y)$},
        minor tick num=1,
        ]
\path let \p1=($(axis cs:0,0,1)-(axis cs:0,0,0)$) in 
\pgfextra{\pgfmathsetmacro{\conv}{2*\y1}
\ifx\gconv\conv
\typeout{z-scale\space good!}
\else
\typeout{Kindly\space consider\space setting\space the\space 
        prefactor\space of\space z\space to\space \conv}
\fi     
        };  
\path let \p1=($(axis cs:1,0,0)-(axis cs:0,0,0)$) in 
\pgfextra{\pgfmathsetmacro{\convx}{veclen(\x1,\y1)}
\typeout{One\space unit\space in\space x\space 
        direction\space is\space\convx pt}
        };                  
\path let \p1=($(axis cs:0,1,0)-(axis cs:0,0,0)$) in 
\pgfextra{\pgfmathsetmacro{\convy}{veclen(\x1,\y1)}
\typeout{One\space unit\space in\space y\space 
        direction\space is\space\convy pt}
        };
  \addplot3 [point meta=0,visualization depends on={
  \gconv*z \as \myz}, % you'll get told how to adjust the prefactor
  scatter/@pre marker code/.append style={/pgfplots/cube/size z=\myz},%
  scatter/@pre marker code/.append style={/pgfplots/cube/size x=24.3018pt},%
  scatter/@pre marker code/.append style={/pgfplots/cube/size y=21.71275pt},%
  scatter,only marks,
  mark=my cube*,mark size=5,opacity=1,domain=0:5,domain y=0:5,samples=6,samples y=6]
  {f(x,y,0.2,0.4,5)};
    \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

请注意,如果您修改这些图,您可能必须重新调整“立方体”的\gconvx尺寸y。但是,代码会告诉您需要使用哪些值。

相关内容