如何绘制由两个相交的高斯分布曲线所界定的区域?

如何绘制由两个相交的高斯分布曲线所界定的区域?

这是我之前的一个问题的后续:填充曲线之间的区域

我想给两条曲线和一条垂直线界定的三个区域上色。每个区域都应该有自己的阴影和颜色。问题是,无论我怎么做,还是做不对。

  1. 第一个区域是从最左边到左曲线下方的垂直线。垂直条的横坐标应为 6。

  2. 第二个区域应为右侧竖线界定的两条曲线下方的公共区域。

  3. 第三个区域上方由右曲线界定,下方由左曲线和右条界定。

下图显示了我期望的输出: 在此处输入图片描述

这是我的尝试:

\documentclass[8pt]{beamer}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usepackage{pgfplots}

\pgfmathdeclarefunction{dnorm}{2}{%
  \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}


\begin{document}

\begin{frame}
  \begin{tikzpicture}
    \begin{axis}[domain=-1.5:12,samples=101,height=8cm,width=12cm, xticklabel=\empty,
    yticklabel=\empty]



\addplot [fill=yellow,draw=none,domain=-1:6,stack plots=y] {max(dnorm(3,1.5)-dnorm(6,1.5),0) } \closedcycle;

\addplot [draw=none,pattern=north west lines,pattern color=orange,domain=-1.5:6,stack plots=y] {min(dnorm(3,1.5),dnorm(6.5,1.5)) } \closedcycle;


\addplot [draw=none,pattern=grid,pattern color=gray,domain=-1.5:6,stack plots=y] {max( dnorm(6,1.5) - dnorm(3,1.5),0)} \closedcycle;


% Draw curves
\addplot [thin, smooth, color=black] {dnorm(3,1.5)};
\addplot [thin, smooth, color=black] {dnorm(6,1.5)};

% Draw vertical bar:
\draw [red, thick] ({rel axis cs:0,0}-|{axis cs:6,0}) -- ({rel axis cs:0,1}-|{axis cs:6,0});

\end{axis}
\end{tikzpicture}

\end{frame}

\end{document}

答案1

我不确定这些是否是您要填充的区域,因此我将填充所有区域,然后您可以选择您需要哪些:

\documentclass[8pt]{beamer}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usepackage{pgfplots}

\pgfmathdeclarefunction{dnorm}{2}{%
  \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}

\begin{document}

\begin{frame}
\begin{tikzpicture}

\begin{axis}[
  domain=-1:12,
  samples=101,
  height=8cm,
  width=12cm, 
  xticklabel=\empty,
  yticklabel=\empty]

\addplot[
  fill=yellow,
  draw=none,
  domain=-1:6,
  stack plots=y
  ] 
  {dnorm(3,1.5)} \closedcycle;

\addplot [
    fill=orange,
    draw=none,
    domain=-1:6,
    stack plots=y
] {min( dnorm(6,1.5) - dnorm(3,1.5),0)} \closedcycle;

\addplot [
    fill=cyan,
    draw=none,
    domain=-1:6,
    stack plots=y
] {max( dnorm(6,1.5) - dnorm(3,1.5),0)} \closedcycle;

\addplot [
    fill=magenta,
    draw=none,
    domain=6:12,
] {max( dnorm(6,1.5),dnorm(3,1.5))} \closedcycle;

\addplot [
    fill=olive,
    draw=none,
    domain=6:12,
] {min( dnorm(6,1.5),dnorm(3,1.5))} \closedcycle;

% Draw curves
\addplot [thin, smooth, color=black] {dnorm(3,1.5)};
\addplot [thin, smooth, color=black] {dnorm(6,1.5)};

% Draw vertical bar:
\draw [red, thick] ({rel axis cs:0,0}-|{axis cs:6,0}) -- ({rel axis cs:0,1}-|{axis cs:6,0});
\end{axis}

\end{tikzpicture}
\end{frame}

\end{document}

在此处输入图片描述

答案2

PSTricks 解决方案使用pst-func包裹:

\documentclass{article}

\usepackage{pst-func}
\usepackage{xfp}

% function
\def\Gauss[#1,#2]#3{\fpeval{1/(sqrt(2*pi)*#2)*exp(-(#3-#1)^2/(2*#2^2))}}

% settings for PSTricks
\psset{
  xunit = 2,
  yunit = 3
}

\begin{document}

\begin{pspicture}(-0.25,-0.15)(4.8,1.8)
% parameters
\def\mueA{2}
\def\mueB{2.7}
\def\sigma{0.4}
% points on curve
\def\pointA{2.1}
\def\pointB{3.1}
  % drawing
  \psaxes[labels = none, ticks = none]{->}(0,0)(4.8,1.8)
  \uput[270](2.4,0){\small Internal Response (a.u.)}
  \rput(0.3,0.7){\psrotate(0,0){90}{\small Probability}}
  \psGauss[mue = \mueA, sigma = \sigma]{0.5}{3.5}
  \pscustom[fillstyle = hlines, hatchsep = 2pt]{%
    \psGauss[mue = \mueB, sigma = \sigma, hatchsep = 1pt]{2.2}{4.4}
    \psline(2.2,0)(2.2,1)}
  \pscustom[fillstyle = vlines]{%
    \psline(2.2,1)(2.2,0)
    \psGauss[mue = \mueB, sigma = \sigma]{0}{2.2}}
  \psline[linewidth = 1.5pt](2.2,0)(2.2,1.3)
  \uput[90](2.2,1.3){\small criterion respons}
  \psline{->}(\fpeval{\pointA-0.3},\fpeval{\Gauss[\mueB,\sigma]{\pointA}+0.2})(\pointA,\Gauss[\mueB,\sigma]{\pointA})
  \uput[90](  \fpeval{\pointA-0.3},\fpeval{\Gauss[\mueB,\sigma]{\pointA}+0.2}){\footnotesize Miss}
  \psline{->}(\fpeval{\pointB+0.3},\fpeval{\Gauss[\mueB,\sigma]{\pointB}+0.2})(\pointB,\Gauss[\mueB,\sigma]{\pointB})
  \uput[90](  \fpeval{\pointB+0.3},\fpeval{\Gauss[\mueB,\sigma]{\pointB}+0.2}){\footnotesize Hit}
\end{pspicture}

\begin{pspicture}(-0.25,-0.15)(4.8,1.8)
% parameters
\def\mueA{2}
\def\mueB{2.7}
\def\sigma{0.4}
% points on curve
\def\pointA{1.6}
\def\pointB{2.8}
  % drawing
  \psaxes[labels = none, ticks = none]{->}(0,0)(4.8,1.8)
  \uput[270](2.4,0){\small Internal Response (a.u.)}
  \rput(0.3,0.7){\psrotate(0,0){90}{\small Probability}}
  \psGauss[mue = \mueB, sigma = \sigma]{0.5}{4.5}
  \pscustom[fillstyle = hlines, hatchsep = 2pt]{%
    \psGauss[mue = \mueA, sigma = \sigma, hatchsep = 1pt]{2.2}{4.4}
    \psline(2.2,0)(2.2,1)}
  \pscustom[fillstyle = vlines]{%
    \psline(2.2,1)(2.2,0)
    \psGauss[mue = \mueA, sigma = \sigma]{0}{2.2}}
  \psline[linewidth = 1.5pt](2.2,0)(2.2,1.3)
  \uput[90](2.2,1.3){\small criterion respons}
  \psline{->}(\fpeval{\pointA-0.3},\fpeval{\Gauss[\mueA,\sigma]{\pointA}+0.2})(\pointA,\Gauss[\mueA,\sigma]{\pointA})
  \uput[120](\fpeval{\pointA-0.3},\fpeval{\Gauss[\mueA,\sigma]{\pointA}+0.2}){\footnotesize Correct rejection}
  \psline{->}(\fpeval{\pointB+0.3},\fpeval{\Gauss[\mueA,\sigma]{\pointB}+0.2})(\pointB,\Gauss[\mueA,\sigma]{\pointB})
  \uput[90](\fpeval{\pointB+0.3},\fpeval{\Gauss[\mueA,\sigma]{\pointB}+0.2}){\footnotesize False alarm}
\end{pspicture}

\end{document}

输出

\pointA请注意,只需分别更改和的值,就可以轻松更改曲线上箭头的位置\pointB

答案3

pgfplots 1.10 版刚刚发布,它为填充图表之间区域的问题提供了新的解决方案。

请注意,旧解决方案仍然可行且有效;此处仅提供可能简化任务的更新。为了使本网站的知识库保持最新,我fillbetween在此提供基于新库的解决方案:

在此处输入图片描述

\documentclass{standalone}

\usepackage{pgfplots}

\usetikzlibrary{patterns}

\pgfplotsset{compat=1.9}

% \usetikzlibrary{}
\usepgfplotslibrary{fillbetween}

\pgfmathdeclarefunction{dnorm}{2}{%
  \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}

\begin{document}
  \begin{tikzpicture}
    \begin{axis}[domain=-3:12,samples=25,height=8cm,width=12cm]

    % Draw curves
    \addplot [name path=g3,thin, smooth, color=black] {dnorm(3,1.5)};
    \addplot [name path=g6,thin, smooth, color=black] {dnorm(6,1.5)};

    % Draw vertical bar:
    \draw [name path=red,red, thick] ({rel axis cs:0,0}-|{axis cs:6,0}) -- ({rel axis cs:0,1}-|{axis cs:6,0});


    \addplot fill between[of=g3 and g6,
        soft clip={domain=-6:6},
        split,
        every segment no 0/.style={yellow},
        every segment no 1/.style={pattern=grid,pattern color=gray},
    ];

    \path[name path=lower,
        %draw=red,ultra thick,
        intersection segments={
            of=g3 and g6,
            sequence=B0 -- A1
        }
    ];
    \path[name path=axis] (axis cs:-10,0) -- (axis cs:16,0);

    \addplot[pattern=north west lines,
        pattern color=orange]
    fill between[
        of=axis and lower,
        soft clip={domain=-6:6}]
    ;

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

g3此解决方案分别用和标记两个相关的输入图g6。第三个\addplot命令使用新的\usepgfplotslibrary{fillbetween}:它计算 之间的填充区域g3 and g6。此外,它将结果剪辑到domain=-6:6(“软剪辑”与标准剪辑非常相似,但针对此用例进行了定制,并且语法简化)。因此,x=6 右侧的所有内容都不属于填充区域。关键split意味着填充区域应分成单独的段,并且每个段都可以获得单独的样式。在我们的例子中,我们分别为第 0 和第 1 个段分配段样式yellow和模式。

以下\path指令计算(但不draw)部分以下交集。它通过新特征实现这一点,新特征intersection segments也是 的一部分\usepgfplotslibrary{fillbetween}。在这种情况下,交集段由 组成,B0它是 的第二个参数的第一个(第 0 个)段g3 and g6,换句话说: 的第一个交集段g6。它将这部分与A1的第一个参数的第二个(第 1 个)部分连接起来of=g3 and g6。这个结果没有绘制也没有填充;它只是与名称 相关联lower

简单\path[name path=axis]地为 x 轴的路径指定一个名称。

最后,最后一个填充和\addplot fill between之间的区域,并用图案填充。请注意,这也有一个将其区域限制为的路径。axislowerfill betweensoft clipx=-6:6

答案4

这是我的最终答案:

\documentclass[8pt]{beamer}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usepackage{pgfplots}

\pgfmathdeclarefunction{dnorm}{2}{%
  \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}

\begin{document}

\begin{frame}
  \begin{tikzpicture}
    \begin{axis}[domain=-3:12,samples=101,height=8cm,width=12cm]

    \addplot [draw=none,pattern=north west lines,pattern color=orange,domain=-3:6,stack plots=y] {dnorm(3,1.5)} \closedcycle;

    \addplot [fill=yellow,draw=none,domain=-3:6,stack plots=y] {min(dnorm(6,1.5)-dnorm(3,1.5),0) } \closedcycle;

    \addplot [draw=none,pattern=grid,pattern color=gray,domain=-3:6,stack plots=y] {max( dnorm(6,1.5) - dnorm(3,1.5),0)} \closedcycle;

    % Draw curves
    \addplot [thin, smooth, color=black] {dnorm(3,1.5)};
    \addplot [thin, smooth, color=black] {dnorm(6,1.5)};

    % Draw vertical bar:
    \draw [red, thick] ({rel axis cs:0,0}-|{axis cs:6,0}) -- ({rel axis cs:0,1}-|{axis cs:6,0});

    \end{axis}
    \end{tikzpicture}

\end{frame}

\end{document} 

在此处输入图片描述

相关内容