# Tikz: function and node's position

With the following code I am able to create three normal distributions with different means. Although the graph looks good, I have some problems in placing the nodes in the right coordinates.

It is obvious that the coordinates of the graph and those of the a4 page are not the same. This is something logical I suppose, so that the graph can be actually created.

In order to put the nodes, I had to follow the trial and error technique until I find the right place on the graph. Of course this does not seem to be an ideal solution.

I hope you can give me some hints of how to deal with the aforementioned.

The code:

\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
\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}

-

You have a few choices here; you could use the axis:cs in the node definition, for example

    \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)$};


which gives

% 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)$};
\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}


Alternatively, you could add the node directly to the addplot command, and specify the pos and anchor, for example:

    \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)$};


which gives

You can play with pos and set it anywhere between 0 and 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}

-
Thank you. I want to ask another question if possible. From the code that I have made, the resulting graph seems to be a little bit small. That being said I would prefer something that "spreads" a little bit more horizontally, so the graphs are not very close to each other. Is there any way to fix that? – Pantelis Kazakis Feb 13 '14 at 22:33
@PantelisKazakis maybe play with width=<whatever you want>? or do you want to mess with the formulas to trick the reader? :) – cmhughes Feb 13 '14 at 22:36
No sir, it is for educational purposes and I want the students to read the information provided easier :) – Pantelis Kazakis Feb 13 '14 at 22:37
@HarishKumar thanks :) you have my vote, too :) – cmhughes Feb 13 '14 at 22:39

You can put the nodes inside the axis environment and use axis cs (axis coordinate system). Then you can put

\node at (axis cs:1.6,0.21) {$f(\theta_A, x)$};


the coordinates you see on the axis.

Code:

\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
\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}


There is also rel axis cs system in which the lower left corner is (0,0) and upper right corner is (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
\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)$};