我正在尝试遮蔽两个高斯重叠的区域。
我的代码如下(根据 Christian 的回答修改而来:如何绘制由两个相交的高斯分布曲线所界定的区域?):
\documentclass[t]{beamer}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfmathdeclarefunction{gauss}{2}{\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}}
\begin{document}
\begin{frame}
\begin{tikzpicture}
\begin{axis}[domain=0:10,samples=100,height=6cm,width=10cm, ytick=\empty,xtick=\empty]
% Draw gaussians
\addplot [name path=model,very thick, smooth, color=red!50!black] {gauss(5,1)};
\addplot [name path=obs,very thick, smooth, color=black] {gauss(5,1.5)};
% calculate region to shade
\path[name path=lower,
intersection segments={
of=model and obs,
sequence=A0 -- B1
}
];
\path[name path=axis] (axis cs:-10,0) -- (axis cs:16,0);
% shade region
\addplot[color=red!20!gray!20]
fill between[
of=axis and lower,
soft clip={domain=1:10}]
;
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}
谢谢,奥利弗
答案1
您必须在代码中将该段添加A2
到sequence
:
sequence=A0 -- B1 -- A2
或者使用较新版本的语法:
sequence={L1 -- R2 --L3}
代码:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\usepgfplotslibrary{fillbetween}
\pgfmathdeclarefunction{gauss}{2}
{\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
domain=0:10,samples=100,
height=6cm,width=10cm,
ytick=\empty,xtick=\empty,
every axis plot/.append style={very thick,smooth}
]
\addplot [name path=model, color=red!50!black] {gauss(5,1)};
\addplot [name path=obs, color=black] {gauss(5,1.5)};
\path[name path=axis] (axis cs:0,0) -- (axis cs:10,0);
\path[name path=lower,
intersection segments={of=model and obs,
sequence={L1 -- R2 --L3}
}];
\addplot[color=red!20!gray!20]
fill between[of= lower and axis];
\end{axis}
\end{tikzpicture}
\end{document}
下面是一张显示所用段的附加图片L1
(橙色)、R2
(蓝色)和L2
(绿色):
代码:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\usepgfplotslibrary{fillbetween}
\pgfmathdeclarefunction{gauss}{2}
{\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
domain=0:10,samples=100,
height=6cm,width=10cm,
ytick=\empty,xtick=\empty,
every axis plot/.append style={very thick,smooth}
]
\addplot [name path=model, color=red!50!black] {gauss(5,1)};
\addplot [name path=obs, color=black] {gauss(5,1.5)};
\path[name path=axis] (axis cs:0,0) -- (axis cs:10,0);
\path[draw,orange,line width=1mm,
intersection segments={of=model and obs,
sequence={L1}
}];
\path[draw,blue,line width=1mm,
intersection segments={of=model and obs,
sequence={R2}
}];
\path[draw,green,line width=1mm,
intersection segments={of=model and obs,
sequence={L3}
}];
\end{axis}
\end{tikzpicture}
\end{document}
答案2
这是一个(不太好的)解决方案/解决方法。
\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfmathdeclarefunction{gauss}{2}{\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[domain=0:10,samples=100,height=6cm,width=10cm, ytick=\empty,xtick=\empty]
\addplot [name path=model,very thick, smooth, color=red!50!black] {gauss(5,1)};
\addplot [name path=obs,very thick, smooth, color=black] {gauss(5,1.5)};
\path[name path=lower,
intersection segments={
of=model and obs,
sequence=A0 -- B1
}
];
\path[name path=axis] (axis cs:-10,0) -- (axis cs:16,0);
\addplot[color=red!20!gray!20]
fill between[
of= model and axis]
;
\addplot[color=white]
fill between[
of=model and obs,
soft clip={domain=1:10}]
;
\end{axis}
\end{tikzpicture}
\end{document}