2

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

\documentclass[tikz, border=1pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\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
            ]
            \addplot [color=black, mark=none, samples=50] {
                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:

goal

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.

1

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}
\pgfplotsset{compat=newest}
\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][]{
\addplot[#1] coordinates {(#2,0) (#2,{gaussian(#2,\std, \mean)})};}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}
            [ 
                axis x line=center,
                axis y line=center,
                ymin=0, ymax=1.5,
                xmin=-3, xmax=3
            ]
            \addplot [color=black, mark=none, samples=50] {
                gaussian(x,\std, \mean)}; 
            \VerticalLine[dashed]{-1}   
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

7
  • Many thanks. Correct me if I'm wrong, but it looks like you can simply remove the "\x" parameter from the gaussian function definition (and all the corresponding calls to it), as it is never actually used. Was there any particular reason you decided to include that? – rish987 Aug 4 '18 at 4:02
  • @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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.