6

I want to place a node label onto a Tikz figure (underneath the axis at a specific point), in order to indicate some interesting behaviours on a selection of smooth curves. The following "Simplified MLE" works perfectly, and gives me exactly what I would like...

\documentclass{minimal}
\usepackage{tikz, pgfplots} 
\begin{document}

    \begin{tikzpicture}
        \draw[dashed] (0,0) -- (0,1); 
        \draw[thick,->] (-1,0) -> (1,0);
        \node[below] (O) at ( 0, 0) {label}; 
    \end{tikzpicture}

\end{document}

Simplified_MWE

However when attempting, what I believe to be the very same task for a more "Complicated MWE" (in this artificial case, I just want to annotate a cord from the x-axis to the maximum point of a Normal Distribution curve), the Tikz label is shifted almost completely outside of the generated image's bounds and is somehow hidden under the "white" axis...

\documentclass{minimal}
\usepackage{tikz, pgfplots}
\usetikzlibrary{calc}
\begin{document}

    \pgfmathsetmacro{\xMin}{-5} \pgfmathsetmacro{\xMax}{5}
    \pgfmathsetmacro{\Mu}{0} \pgfmathsetmacro{\Sigma}{1}

    \begin{tikzpicture}[declare function={Normal(\x,\MU,\SIG) = 1/(\SIG*sqrt(2*pi))*exp(-0.5*(pow(\x-\MU,2)));}]
        \begin{axis}[every axis plot post/.append style={domain={\xMin}:{\xMax}, samples=100, mark=none, smooth}, 
            axis lines=left, hide y axis, xtick=\empty, ytick=\empty, enlargelimits=upper] 
            \addplot[color=red]{Normal(\x, {\Mu}, {\Sigma})}; 
            \addplot[color=red, dashed] coordinates {({\Mu}, 0) ({\Mu}, {Normal({\Mu}, {\Mu}, {\Sigma})})};
            \node[color=black] (MuLabel) at ( {\Mu}, 0) {$\mu_{1}$}; 
        \end{axis}
    \end{tikzpicture}

\end{document}

Complicated_MWE

Admittedly, this is my first evening playing around with the Tikz package, so chances are I'm missed something obvious or included something stupid; my question is what in the latter example above is making the "Complicated MWE" label's behaviour so drastically different to that of the "Simplified MWE"?

1 Answer 1

5

You need to use axis cs: for tikz coordinates within a pgfplots axis environment, and clip=false:

enter image description here

Reference:

Code:

\documentclass{article}
\usepackage{tikz, pgfplots}
\usetikzlibrary{calc}
\begin{document}

    \pgfmathsetmacro{\xMin}{-5} \pgfmathsetmacro{\xMax}{5}
    \pgfmathsetmacro{\Mu}{0} \pgfmathsetmacro{\Sigma}{1}

    \begin{tikzpicture}[declare function={Normal(\x,\MU,\SIG) = 1/(\SIG*sqrt(2*pi))*exp(-0.5*(pow(\x-\MU,2)));}]
        \begin{axis}[every axis plot post/.append style={domain={\xMin}:{\xMax}, samples=100, mark=none, smooth}, 
            axis lines=left, hide y axis, xtick=\empty, ytick=\empty, enlargelimits=upper,clip=false] 
            \addplot[color=red]{Normal(\x, {\Mu}, {\Sigma})}; 
            \addplot[color=red, dashed] coordinates {({\Mu}, 0) ({\Mu}, {Normal({\Mu}, {\Mu}, {\Sigma})})};
            \node[color=black, below] (MuLabel) at (axis cs: {\Mu}, 0) {$\mu_{1}$}; 
        \end{axis}
    \end{tikzpicture}

\end{document}
1
  • Amazing, thanks so much! Also, cheers for the "Why would the minimal class be avoided?" reference; definitely useful to know for the future... May 8, 2014 at 0:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .