# Draw line from axis to declared function curve

I have the following code, which displays a normal distribution:

\documentclass[tikz, border=1pt]{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\pgfmathdeclarefunction{gaussian}{2}
{%
\pgfmathparse{(1/(#1 * sqrt(2 * pi))) * exp((-1 / 2) * (((x - #2)/#1) ^ 2))}%
}
\pgfmathsetmacro{\mean}{0}
\pgfmathsetmacro{\std}{1}

\begin{document}
\begin{tikzpicture}
\begin{axis}
[
axis x line=center,
axis y line=center,
ymin=0, ymax=1.5,
xmin=-3, xmax=3
]
gaussian(\std, \mean)};
\end{axis}
\end{tikzpicture}
\end{document}


What I want to do is create a command that takes a standard deviation, mean, and x-value as input, and draws a line from the x-axis to the curve, resulting in a plot like this:

Ideally, I want to do this without having to duplicate the math in the pgfmathdeclarefunction section, so if there was some way for me to evaluate that function given the standard deviation, mean, and x-value, that would definitely help. It seems like pgfplots is already doing this in plotting the function, so this should be simple in principle, but oddly enough I haven't managed to find a way to do this.

That's actually rather straightforward, especially if one slightly adjusts the definition of the Gaussian. I made it a function of three (instead of two) argument, x, the standard deviation or width, and the mean. All you need to do to get the vertical line is to say

\addplot[<style>] coordinates {(<x>,0) (<x>,{gaussian(<x>,<width>, <mean>)})};


which of case can be made a macro.

\documentclass[tikz, border=1pt]{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\tikzset{declare function={gaussian(\x,\y,\z)=(1/(\y * sqrt(2 * pi))) * exp((-1
/ 2) * (((x - \z)/\y) ^ 2));}}
\pgfmathsetmacro{\mean}{0}
\pgfmathsetmacro{\std}{1}
\newcommand{\VerticalLine}[2][]{
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
axis x line=center,
axis y line=center,
ymin=0, ymax=1.5,
xmin=-3, xmax=3
]

• @rish987 Of course it is used. Just try \VerticalLine[dashed]{0.5}. The Gaussian is a function of x (the mean and width). – user121799 Aug 4 '18 at 4:05
• I removed the "\x" parameter (note that this is distinct from the "x" parameter with no slash in front, which is used in the function) to no ill effect. The call \VerticalLine[dashed]{0.5} still drew a line that ended at the curve. – rish987 Aug 4 '18 at 4:19
• @rish987 \x is just a placeholder. You could name it \CuteFurryRodent or whatever. – user121799 Aug 4 '18 at 4:21
• Correspondingly, I changed the command to \addplot[#1] coordinates {(#2,0) (#2,{gaussian(\std, \mean)})}; It seems like, in the second pair of coordinates, the x-position is used as an implicit 'x' parameter to the gaussian function. – rish987 Aug 4 '18 at 4:22