我正在尝试使用 tikz 中的 pgfplots 绘制高斯分布。我想画一条水平线来表示标准偏差,在这个例子中是一条水平线,位于 x=0.5 - 1.5,y = 函数在这些位置相等的值。
我该如何做呢?
现在我有代码可以给出如下信息:
%plot of gaussian distribution
\documentclass[tikz]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
%gauss function {loc, std}
\pgfmathdeclarefunction{gaussx}{2}{%
\pgfmathparse{exp(-#2*((x-#1)^2)}%
}
%gauss function {x, loc, std}
\pgfmathdeclarefunction{gauss}{3}{%
\pgfmathparse{exp(-#3*((#1-#2)^2)}%
}
\begin{document}
\begin{tikzpicture}[scale=1.5]
\begin{axis}[
no markers, domain=-6:6,
samples=100,
axis lines*=middle,
xlabel=$x$,ylabel=$e^{-\lambda (x-a)^2}$,
height=5cm, width=7cm,
xtick=\empty, ytick=\empty,
restrict y to domain=--6:6]
\addplot [very thick, teal] {gaussx(1, 1)};
\draw[thin, red] (axis cs:1,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:1,\pgfkeysvalueof{/pgfplots/ymax}); %draws a vertical line, but at x=4
%I thought what I want should look like this?
%\draw[thin, red] (axis cs:1, gauss{1.5, 1, 1}\pgfmathresult)
% -- (axis cs:1, gauss{.5, 1, 1}\pgfmathresult);
\end{axis}
\end{tikzpicture}
\end{document}
我认为它将是我想要的命令的正确语法的一部分,但如果不是,如果您能帮助我使垂直线终止于峰值,我将获得加分。
答案1
\documentclass[tikz]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
%gauss function {loc, std}
\pgfmathdeclarefunction{gaussx}{2}{%
\pgfmathparse{exp(-#2*((x-#1)^2)}%
}
%gauss function {x, loc, std}
\pgfmathdeclarefunction{gauss}{3}{%
\pgfmathparse{exp(-#3*((#1-#2)^2)}%
}
\begin{document}
\begin{tikzpicture}[scale=1.5]
\begin{axis}[
no markers, domain=-6:6, samples=100,
axis lines*=middle,
xlabel=$x$,ylabel=$e^{-\lambda (x-a)^2}$,
every axis y label/.style={at=(current axis.west),anchor=south,rotate=90},
every axis x label/.style={at=(current axis.south),anchor=north},
height=5cm, width=7cm,
xtick=\empty,
ytick=\empty,
enlargelimits=false,
% clip=false,
axis on top,
grid = major]
\addplot [very thick, teal] {gaussx(1, 1)};
\addplot [fill=none, draw=blue, domain=0.5:1.5] {gaussx(1,1)} \closedcycle;
\draw[red] (axis cs:1,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:1,\pgfkeysvalueof{/pgfplots/ymax});
\end{axis}
\end{tikzpicture}
\end{document}
I f