使用以下代码,我可以创建三个具有不同均值的正态分布。虽然图表看起来不错,但我在将节点放置在正确的坐标上时遇到了一些问题。
很明显,图表的坐标和 A4 纸的坐标不一样。我认为这是合乎逻辑的,这样图表才能真正被创建出来。
为了放置节点,我不得不遵循反复试验的方法,直到在图表上找到正确的位置。当然,这似乎不是一个理想的解决方案。
我希望您能给我一些关于如何处理上述问题的提示。
代码:
\documentclass[hidelinks,parskip=full,12pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{intersections}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\begin{document}
\begin{center}
\newcommand\gauss[2]{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}
\begin{tikzpicture}
\begin{axis}[every axis plot post/.append style={
mark=none,domain=-5:20,samples=50,smooth},
axis x line*=bottom, % no box around the plot, only x and y axis
axis y line*=left, % the * suppresses the arrow tips
enlargelimits=upper] % extend the axes a bit to the right and top
\addplot {\gauss{2}{2}};
\addplot {\gauss{8}{2}};
\addplot {\gauss{14}{2}};
\end{axis}
\draw (1.55, 5.6) node{$f(\theta_A, x)$};
\draw (3.50, 5.6) node{$f(\theta_B, x)$};
\draw (5.50, 5.6) node{$f(\theta_C, x)$};
\end{tikzpicture}
\end{center}
\end{document}
答案1
这里有几个选择;例如,你可以axis:cs
在定义中使用node
\node[anchor=south] at (axis cs:0,0.2) {$f(\theta_A, x)$};
\node[anchor=south] at (axis cs:6,0.2) {$f(\theta_B, x)$};
\node[anchor=south] at (axis cs:15,0.2) {$f(\theta_C, x)$};
这使
% arara: pdflatex
% !arara: indent: {overwrite: on}
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\begin{document}
\newcommand\gauss[2]{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}
\begin{tikzpicture}
\begin{axis}[every axis plot post/.append style={
mark=none,domain=-5:20,samples=50,smooth},
axis x line*=bottom, % no box around the plot, only x and y axis
axis y line*=left, % the * suppresses the arrow tips
enlargelimits=upper] % extend the axes a bit to the right and top
%\addplot {\gauss{2}{2}}node[pos=0.5]{$f(\theta_A, x)$};
\addplot {\gauss{2}{2}};
\addplot {\gauss{8}{2}};
\addplot {\gauss{14}{2}};
\node[anchor=south] at (axis cs:0,0.2) {$f(\theta_A, x)$};
\node[anchor=south] at (axis cs:6,0.2) {$f(\theta_B, x)$};
\node[anchor=south] at (axis cs:15,0.2) {$f(\theta_C, x)$};
\end{axis}
\end{tikzpicture}
\end{document}
或者,您可以将node
直接添加到addplot
命令中,并指定pos
和anchor
,例如:
\addplot {\gauss{2}{2}}node[pos=0.2,anchor=east]{$f(\theta_A, x)$};
\addplot {\gauss{8}{2}}node[pos=0.1,anchor=south west]{$f(\theta_B, x)$};
\addplot {\gauss{14}{2}}node[pos=0.9]{$f(\theta_C, x)$};
这使
您可以pos
随意设置0
它1
% arara: pdflatex
% !arara: indent: {overwrite: on}
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\begin{document}
\newcommand\gauss[2]{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}
\begin{tikzpicture}
\begin{axis}[every axis plot post/.append style={
mark=none,domain=-5:20,samples=50,smooth},
axis x line*=bottom, % no box around the plot, only x and y axis
axis y line*=left, % the * suppresses the arrow tips
enlargelimits=upper] % extend the axes a bit to the right and top
\addplot {\gauss{2}{2}}node[pos=0.2,anchor=east]{$f(\theta_A, x)$};
\addplot {\gauss{8}{2}}node[pos=0.1,anchor=south west]{$f(\theta_B, x)$};
\addplot {\gauss{14}{2}}node[pos=0.9]{$f(\theta_C, x)$};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
您可以将node
s 放在环境中axis
并使用axis cs
(轴坐标系)。然后您可以放置
\node at (axis cs:1.6,0.21) {$f(\theta_A, x)$};
您在轴上看到的坐标。
代码:
\documentclass[hidelinks,parskip=full,12pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{intersections}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\begin{document}
\begin{center}
\newcommand\gauss[2]{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}
\begin{tikzpicture}
\begin{axis}[every axis plot post/.append style={
mark=none,domain=-5:20,samples=50,smooth},
axis x line*=bottom, % no box around the plot, only x and y axis
axis y line*=left, % the * suppresses the arrow tips
enlargelimits=upper] % extend the axes a bit to the right and top
\addplot {\gauss{2}{2}};
\addplot {\gauss{8}{2}};
\addplot {\gauss{14}{2}};
\node at (axis cs:1.6,0.21){$f(\theta_A, x)$};
\node at (axis cs:7.5,0.21){$f(\theta_B, x)$};
\node at (axis cs:14,0.21){$f(\theta_C, x)$};
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
还有rel axis cs
一种系统,左下角是(0,0)
,右上角是(1,1)
\documentclass[hidelinks,parskip=full,12pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{intersections}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\begin{document}
\begin{center}
\newcommand\gauss[2]{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}
\begin{tikzpicture}
\begin{axis}[every axis plot post/.append style={
mark=none,domain=-5:33,samples=50,smooth},
axis x line*=bottom, % no box around the plot, only x and y axis
axis y line*=left, % the * suppresses the arrow tips
enlargelimits=upper,width=5in] % extend the axes a bit to the right and top
\addplot {\gauss{2}{2}};
\addplot {\gauss{14}{2}};
\addplot {\gauss{25}{2}};
\node at (rel axis cs:0.17,0.95){$f(\theta_A, x)$};
\node at (rel axis cs:0.46,0.95){$f(\theta_B, x)$};
\node at (rel axis cs:0.71,0.95){$f(\theta_C, x)$};
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}