# [Tex/LaTex] How to plot a graph of arbitrary points in TikZ

tikz-pgf

I have this code:

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,decorations.pathmorphing,backgrounds,positioning,fit,petri}

\begin{document}
\begin{tikzpicture}

[
place/.style={circle,draw=red,fill=red,thick}]

\draw[help lines,->] (0,0) -- (10,0);
\draw[help lines,->] (0,0) -- (0,10);   % draw axis lines
\draw[gray,dashed] (0,10) -- (10,10);   % draw asymptote

% place points on the graph

\node at (1,2) [place] {};
\node at (2,1) [place] {};
\node at (3,0.5) [place] {};
\node at (4,0.25) [place] {};
\node at (5,7) [place] {};
\node at (6,6) [place] {};
\node at (7,2) [place] {};
\node at (8,4) [place] {};
\node at (9,9) [place] {};
\node at (10,9.8) [place] {};

\end{tikzpicture}
\end{document}


Which produces this result:

I have two questions:

1. What I wanted was for each point on the graph to be represented by a solid red circle, not an unfilled black circle. (See code above.) What did I do wrong?

2. The next step here is to connect those points with a curve. What is the best way to do so in tikz?

Remove all spaces when declaring the options for your tikzpicture and you'll get the nodes displayed properly.

To connect the nodes, assign them names containing increasing numbers and then draw the lines easily with a \foreach statement.

\documentclass{standalone}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[place/.style={circle,draw=red,fill=red,thick}]
\draw[help lines,->] (0,0) -- (10,0);
\draw[help lines,->] (0,0) -- (0,10);
\draw[gray,dashed] (0,10) -- (10,10);
\node (n1) at (1,2) [place] {};
\node (n2) at (2,1) [place] {};
\node (n3) at (3,0.5) [place] {};
\node (n4) at (4,0.25) [place] {};
\node (n5) at (5,7) [place] {};
\node (n6) at (6,6) [place] {};
\node (n7) at (7,2) [place] {};
\node (n8) at (8,4) [place] {};
\node (n9) at (9,9) [place] {};
\node (n10) at (10,9.8) [place] {};

\foreach \x [evaluate={\y=int(\x+1);}] in {1,...,9}
\draw (n\x) -- (n\y);
\end{tikzpicture}
\end{document}


In this very case the code could be shortened as follows:

\documentclass{standalone}

\usepackage{tikz}
\usepackage{ifthen}

\begin{document}
\begin{tikzpicture}[place/.style={circle,fill=red,thick}]
\foreach \x/\y in {1/2, 2/1, 3/0.5, 4/0.25, 5/7, 6/6, 7/2, 8/4, 9/9, 10/9.8} {
\node (n\x) at (\x,\y) [place] {};
}
\foreach \x [evaluate={\y=int(\x+1);}] in {1,...,9} {
\draw (n\x) -- (n\y);
}
\end{tikzpicture}
\end{document}


BUT, if you are "only" trying to draw a plot and don't want to mess with nodes, then you may consider using pgfplots:

\documentclass{standalone}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
title=Title,
xlabel=$x$ coordinate,
ylabel=$y$ coordinate,]
\addplot coordinates {(1,2) (2,1) (3,0.5) (4,0.25) (5,7) (6,6) (7,2) (8,4) (9,9) (10,9.8)};
% if you want the plot to be RED, instead write: \addplot [red,mark=*] coordinates ...
\end{axis}
\end{tikzpicture}
\end{document}