填充正态分布曲线下的面积

填充正态分布曲线下的面积

在此处输入图片描述

我正在尝试填充两个 $z$ 分数之间的区域,但没有成功。有人能帮我吗?

\begin{tikzpicture}
\begin{axis}[
  no markers, 
  domain=0:6, 
  samples=100,
  ymin=0,
  axis lines*=left, 
  every axis y label/.style={at=(current axis.above origin),anchor=south},
  every axis x label/.style={at=(current axis.right of origin),anchor=west},
  height=5cm, 
  width=12cm,
  xtick=\empty, 
  ytick=\empty,
  enlargelimits=false, 
  clip=false, 
  axis on top,
  grid = major,
  hide y axis
  ]
 \addplot [very thick,cyan!50!black] {gauss(x, 3, 1)};

\pgfmathsetmacro\valueB{gauss(1,1,7)}
\pgfmathsetmacro\valueA{gauss(1,1,1.7)}
\pgfmathsetmacro\valueC{gauss(1,1,1)}

\draw [very thick, red]  (axis cs:1,0) -- (axis cs:1,\valueB);
\draw [very thick, red]  (axis cs:2,0) -- (axis cs:2,\valueA);
\draw [very thick, red]  (axis cs:3,0) -- (axis cs:3,\valueC);

\node[below] at (axis cs:3, 0)  {$\mu$}; 
\node[below] at (axis cs: 2, 0) {$z=-1.8$};
\node[below] at (axis cs: 1,0) {$z=-2.97$};
\end{axis}
\end{tikzpicture}

答案1

添加

\addplot [draw=none, fill=yellow!25, domain=1:2] {gauss(x, 3, 1)} \closedcycle;

\addplot应该这么做。

在此处输入图片描述

答案2

PSTricks 解决方案:

\documentclass{article}

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

% constants
\newcommand*\constA{\fpeval{round(1/\Sigma,2)}}
\newcommand*\constB{\fpeval{round(2*\Sigma^2,2)}}

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

% labels
\newcommand*\LabelA[1]{%
  \psline(#1,-0.02)(#1,0.02)
  \uput[90](\fpeval{#1-0.25},0.25){\footnotesize $z = #1$}
  \psline[linewidth = 0.02]{->}(\fpeval{#1-0.25},0.25)(#1,0)}
\newcommand*\LabelB[1]{%
  \psline(#1,-0.02)(#1,0.02)
  \uput[90](\fpeval{#1+0.25},0.25){\footnotesize $z = #1$}
  \psline[linewidth = 0.02]{->}(\fpeval{#1+0.25},0.25)(#1,0)}

% settings
\psset{
  xunit = 2,
  yunit = 5
}

% parameters
\def\Mue{2.5}
\def\Sigma{0.44}
\def\pointA{1.5}
\def\pointB{2.2}


\begin{document}

\begin{pspicture}(-0.35,-0.1)(5.05,1.2)
  \pscustom[linestyle = none, fillstyle = solid, fillcolor = red!80]{%
    \psline(\pointA,0)(\pointA,\Gauss[\Mue,\Sigma]{\pointA})
    \psGauss[mue = \Mue, sigma = \Sigma]{\pointA}{\pointB}
    \psline(\pointB,\Gauss[\Mue,\Sigma]{\pointB})(\pointB,0)}
  \psaxes[Dx = 0.5, Dy = 0.2]{->}(0,0)(0,0)(4.9,1.1)[$z$,0][$P(z)$,90]
  \psline[linecolor = red!80](\Mue,\Gauss[\Mue,\Sigma]{\Mue})(\Mue,0)
  \psGauss[mue = \Mue, sigma = \Sigma, linecolor = blue!80]{0.25}{4.75}
%  \rput(\fpeval{\Mue+1.3},\fpeval{\Gauss[\Mue,\Sigma]{\Mue}-0.1}){%
%    $(\mu,\sigma) = (\Mue,\Sigma)$}
  \rput(\Mue,\fpeval{\Gauss[\Mue,\Sigma]{\Mue}+0.2}){%
    $P(z) = {\displaystyle\frac{\constA}{\sqrt{2\pi}}}
             \exp{\mkern -8mu}\left(-\frac{(z-\Mue)^{2}}{\constB}\right)$}
  \LabelA{\pointA}
  \LabelB{\pointB}
\end{pspicture}

\end{document}

输出

答案3

另一种选择是通过命令使用scope环境\clip

\begin{scope}[yshift=-\pgflinewidth]
\clip (axis cs:1,0) rectangle (axis cs:2,0.24);
\addplot [draw=none,fill=blue] {gauss(x, 3, 1)};
\end{scope}

在此处输入图片描述

代码

\documentclass[border=2cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\begin{document}

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

\begin{tikzpicture}
\begin{axis}[
  no markers, 
  domain=0:6, 
  samples=100,
  ymin=0,
  axis lines*=left, 
  every axis y label/.style={at=(current axis.above origin),anchor=south},
  every axis x label/.style={at=(current axis.right of origin),anchor=west},
  height=5cm, 
  width=12cm,
  xtick=\empty, 
  ytick=\empty,
  enlargelimits=false, 
  clip=false, 
  axis on top,
  grid = major,
  hide y axis
  ]

\begin{scope}[yshift=-\pgflinewidth]
\clip (axis cs:1,0) rectangle (axis cs:2,0.24);
\addplot [draw=none,fill=blue] {gauss(x, 3, 1)};
\end{scope}

\addplot [very thick,cyan!50!black] {gauss(x, 3, 1)};
\pgfmathsetmacro\valueB{gauss(1,1,7)}
\pgfmathsetmacro\valueA{gauss(1,1,1.65)}
\pgfmathsetmacro\valueC{gauss(1,1,1)}

\draw [very thick, red]  (axis cs:1,0) -- (axis cs:1,\valueB);
\draw [very thick, red]  (axis cs:2,0) -- (axis cs:2,\valueA);
\draw [very thick, red]  (axis cs:3,0) -- (axis cs:3,\valueC);

\node[below] at (axis cs:3,0)  {$\mu$}; 
\node[below] at (axis cs:2,0) {$z=-1.8$};
\node[below] at (axis cs:1,0) {$z=-2.97$};
\end{axis}

\end{tikzpicture}
\end{document}

相关内容