4

I would like to have your input on the following example. I simply want to show a few points, draw connections between them and label/color them accordingly.

Using TSX I came up with the following but am not sure if this is -- in the long run -- the best way. I like the tupel based input, but would like to use names to simply drawing the line between them. Can the example be upgraded to include something like this?

enter image description here

\documentclass[12pt,english]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz,pgfplots}
\setbeamertemplate{navigation symbols}{}

\begin{document}
\begin{frame}[plain]
\hspace*{-1.5em}\begin{tikzpicture}
\begin{axis}[
    domain=0:10,
    axis lines = center,
    height=10cm, width=12cm, grid=major,grid style={dashed, gray!30},
    xmin=0, xmax=12, ymin=0, ymax=9,xtick={1,2,...,11},ytick={1,2,...,10}]
    \addplot+[red,only marks,mark=*,mark options={scale=2, fill=red},text mark as node=true] coordinates {
    (1,2)
    (3,1)
    (4,3)
    (5,2)
    (2,3)    
    };
    \addplot+[red,only marks,mark=*,mark options={scale=2, fill=red},text mark as node=true] coordinates {
    (7,7)
    (8,8)
    (9,5)
    (11,6)
    (10,8)
    };

\draw (axis cs:0,0) -- (axis cs:2,2);
\filldraw (axis cs:5,5) circle (3pt);
\node[anchor=north] at (axis cs:7,6.9) {1};
\node[anchor=south] at (axis cs:5,1) {$f(\theta_A, x)$};
\end{axis}

\end{tikzpicture}
\end{frame}
\end{document}
  • 1
    What do you mean when you say "...but would like to use names to simply drawing the line between them"? – Alenanno Aug 28 '16 at 14:17
  • Like node A is on (1,0), B is on (2,2), draw line from A to B. – Uwe Ziegenhagen Aug 28 '16 at 14:23
  • 2
    Where are these nodes in your code? I don't see anything with those coordinates. – Alenanno Aug 28 '16 at 14:27
  • The coordinates were just examples. Maybe my question can be expressed as "Is there a way to assign labels when using the addplot (x,y) tupel syntax?" – Uwe Ziegenhagen Aug 28 '16 at 15:44
  • So do you want to link the red dots? If so, all of them or arbitrarily? – Alenanno Aug 28 '16 at 15:47
1

Well, if you want to simply name the circles so you can act on them, you would need to use nodes, because simple shapes do not have aliases. An easy solution to achieve this, in my opinion, would be to switch to regular Tikz. With a \foreach, you can create many nodes and assign names to each using a counter, which in our case will be like n\n, resulting in nodes named n1, n2, n3 and so on.

Output

enter image description here

Code

\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}

\newcommand\labelit[3][below]{%
    \node[label={#1:#3}] at (#2) {};
}

\def\maxc{11}

\begin{document}
\begin{tikzpicture}
\draw[-Stealth, shorten >=-1cm] (0,0) -- (\maxc,0);
\draw[-Stealth, shorten >=-1cm] (0,0) -- (0,\maxc-2);
\foreach \x [count=\y] in {1,...,\maxc}{
    \draw (\x,.1) -- (\x,-.1) node[below] {\x};
    \ifnum\x<10\relax
    \draw (.1,\y) -- (-.1,\y) node[left] {\y};
    \draw[dashed, gray!30] (0,\y) -- (\maxc+1,\y);
    \fi
    \draw[dashed, gray!30] (\x,0) -- (\x,\maxc-1);
}

\foreach \point [count=\n] in {%
    (1,2),  (3,1), 
    (4,3),  (5,2), 
    (2,3),  (7,7),
    (8,8),  (9,5),
    (11,6), (10,8)
}{%
    \node[circle, fill=red, inner sep=3pt] (n\n) at \point {};
}

% label points, arguments are: [position]{node}{label}
\labelit{n4}{$f(\theta_A, x)$}

% and now random links!
\draw (n5) -- (n3) -- (n1) -- (n4) -- (n2);
\end{tikzpicture}
\end{document}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.