我想将节点标签放置在 Tikz 图形上(特定点的轴下方),以便指示所选平滑曲线上的一些有趣行为。以下“简化 MLE”运行完美,并完全满足我的要求...
\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}
然而,当尝试时,我认为对于更“复杂的 MWE”来说是同样的任务(在这种人为的情况下,我只想注释从 x 轴到正态分布曲线的最大点的线),Tikz 标签几乎完全移到了生成的图像的边界之外,并以某种方式隐藏在“白色”轴下...
\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}
无可否认,这是我第一次使用 Tikz 包,所以很有可能我错过了一些显而易见的东西,或者包含了一些愚蠢的东西;我的问题是,在上面的后一个例子中,是什么让“复杂的 MWE”标签的行为与“简化的 MWE”有如此大的不同?
答案1
您需要在环境内使用坐标axis cs:
,并且:tikz
pgfplots
axis
clip=false
参考:
代码:
\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}