![在高斯图中包括箭头](https://linux22.com/image/466957/%E5%9C%A8%E9%AB%98%E6%96%AF%E5%9B%BE%E4%B8%AD%E5%8C%85%E6%8B%AC%E7%AE%AD%E5%A4%B4.png)
我想复制这个数字
为此,我认为:
- 绘制高斯,
- 绘制箭头
这是我绘制左图的失败尝试:
\documentclass{article}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary {arrows.meta}
\usepackage{mathtools}
\begin{document}
% We define the function we are plotting here. Call it "Gauss"
\pgfmathdeclarefunction{gauss}{2}{%
\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
% Begin plotting
\begin{tikzpicture}
\begin{axis}[every axis plot post/.append style={mark=none,domain=-2:3,samples=50,smooth}, % All plots: from -2:2, 50 samples, smooth, no marks
axis x line*=none, % no box around the plot, only x and y axis
axis y line*=none, % the * suppresses the arrow tips
axis line style={draw=none},
dash pattern=on 4pt off 1pt on 4pt off 4pt,
enlargelimits=upper] % extend the axes a bit to the right and top
\addplot {gauss(0,0.5)};
\end{axis}
\end{tikzpicture}
\end{document}
我想添加箭头
\tikz{
\draw [-{Stealth[length=5mm]}] (0,0) -- (2,0);
}
但我不知道如何将其添加到图中。特别是如何确保箭头指向驼峰的顶峰。
答案1
这是一个lualatex
使用内置的版本元帖子语言。
宏cutafter
在这里派上用场了。您需要使用 编译此源lualatex
。请点击上面的链接了解有关 Metapost 的更多信息。
\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
numeric _sqrtpp; _sqrtpp = 2.50662827463; % sqrt(2pi)
vardef gauss(expr mu, sigma, x) =
if abs(x - mu) < 4 sigma:
mexp(-128 * (((x - mu) / sigma) ** 2)) / _sqrtpp / sigma
else:
0
fi
enddef;
vardef gauss_curve(expr mu, sigma, a, b, s) =
(a, gauss(mu, sigma, a)) for x = a + s step s until b: .. (x, gauss(mu, sigma, x)) endfor
enddef;
vardef Z = gauss_curve(0, 1, -4, 4, 1/8) enddef;
vardef dotarrow(expr p) = image(drawarrow p; draw point 0 of p withpen pencircle scaled ahlength) enddef;
beginfig(1);
numeric u; u = 21;
path base, curve;
base = (left -- right) scaled 5u;
curve = Z xscaled u yscaled 8u;
picture P[];
P0 = image(draw base; draw up--down; draw curve dashed evenly scaled 1/2);
P1 = image(
draw P0;
for x=-2, -1, 0, 1, 2:
draw dotarrow((x*u, 0) -- (x*u, infinity) cutafter curve);
endfor
);
P2 = image(
draw P0;
for x=-1.8, -0.6, 0.6, 1.8:
draw dotarrow((x*u, 0) -- (x*u, infinity) cutafter curve);
endfor
);
draw P1 shifted 120 left;
draw P2 shifted 120 right;
endfig;
\end{mplibcode}
\end{document}