在 tikz/pgf 图的左侧放置一个标签

在 tikz/pgf 图的左侧放置一个标签

我有一个情节,我通过\addplot,我想在函数图的左侧设置一个标签在函数的特定 x 位置。这怎么可能?我试过了note[pos = ...],但它只是将节点放在 -axis 上方x,而不是函数图上方(这是我想要的)。这是我的示例代码:

\documentclass{article}
\usepackage{graphicx}
\usepackage{pgfplots, tikz}
\usetikzlibrary{arrows, calc, positioning}

\begin{document}

\pgfmathdeclarefunction{normal_distribution}{2}{%
    \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}

\section{}

\begin{figure}
    \centering
    \begin{tikzpicture}
        \begin{axis}[domain=-6:6, axis lines*=left, xlabel=$x$, samples=1000, scale only axis, width = \textwidth]
            \addplot [very thick, blue!50!black] {normal_distribution} node[pos = 0.5, above]{$f(x)$};
        \end{axis}
    \end{tikzpicture}
\end{figure}
\end{document}

答案1

也许像这样?

\documentclass{article}
\usepackage{pgfplots, tikz}
\usetikzlibrary{arrows, calc, positioning, intersections}

\pgfmathdeclarefunction{normal_distribution}{2}{%
    \pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}

\begin{document}

\section{}

\begin{figure}
    \centering
    \begin{tikzpicture}
        \begin{axis}[
            domain=-6:6, 
            axis lines*=left, 
            xlabel=$x$, 
            samples=100,
            scale only axis, 
            width=\textwidth,
            no markers
        ]
            \addplot [very thick, blue!50!black, name path=plot] {normal_distribution(0,1)};

            % x where you want the label
            \pgfmathsetmacro\labelx{-2}

            % invisible line at this coordinate
            \path[name path=vertical line] (axis cs:\labelx,-10) -- (axis cs:\labelx,10);

            % intersection to place the node
            \draw [
                name intersections={of=vertical line and plot, by=point},
                draw=none
            ] (point) node [left] {$f(x)$};
        \end{axis}
    \end{tikzpicture}
\end{figure}
\end{document}

在此处输入图片描述

相关内容