[Tex/LaTex] Drawing vertical lines on a coordinate plane using Tikz

tikz-pgf

I am trying to plot the vertical lines x=0 and x=4 using the Tikz environment. What I have thus far is;

\begin{tikzpicture}[yscale=1] 
% 4x4 grid
\draw (0, 0) grid (5, 5);
% origin point
\draw [color=blue, fill=black] (0, 0) circle (0.1);
\draw [color=blue, fill=blue] (1, 1) circle (0.1);
\draw [color=blue, fill=blue] (2, 1) circle (0.1);
\draw [color=blue, fill=blue] (3, 1) circle (0.1);
\draw [color=blue, fill=blue] (1, 2) circle (0.1);
\draw [color=blue, fill=blue] (2, 2) circle (0.1);
\draw [color=blue, fill=blue] (3, 2) circle (0.1);
\draw [color=blue, fill=blue] (1, 2) circle (0.1);
\draw [color=blue, fill=blue] (1, 3) circle (0.1);
\draw [color=blue, fill=blue] (2, 3) circle (0.1);
\draw [color=blue, fill=blue] (3, 3) circle (0.1);
\node at (4,-.5) {$\frac{1}{2}$};
\node at (-.5,4) {$\frac{1}{2}$};
 % x-axis
\draw [thick,->] (0, 0) -- (5.5, 0);
 % y-axis
\draw [thick,->] (0, 0) -- (0, 5.5);
% origin label
\node at (-0.1, -0.5) {(0, 0)};
%Labeling of points
\node at (1.25, 3.25) {$P_{1}$};
\node at (2.25, 3.25) {$P_{2}$};
\node at (3.25, 3.25) {$P_{3}$};
\node at (1.25, 2.25) {$P_{4}$};
\node at (2.25, 2.25) {$P_{5}$};
\node at (3.25, 2.25) {$P_{6}$};
\node at (1.25, 1.25) {$P_{7}$};
\node at (2.25, 1.25) {$P_{8}$};
\node at (3.25, 1.25) {$P_{9}$};
\draw [green, thick, domain=0:4] plot (\x, {0});
\draw [green, thick, domain=0:4] plot (\x, {4});
% x-axis label
\node at (6, 0) {x};
% y-axis label
\node at (0, 6) {y};
\end{tikzpicture}

The commands plot (\x {0}) and plot (\x {4}) produce the lines y=0 and y=4. I had thought that the command plot (\y {0}) and plot (\y {4} would produce the lines needed, but I only receive an error. Is there a straightforward way to plot these lines?

Best Answer

In addition to the solutions provided in the comments, because of the repetitive pattern being found, the code can be simplified by using foreach loop and a counter called sub for subscript of labels. Many line codings are, therefore, reduced significantly by this skill.

enter image description here

Code

\documentclass{article}
\usepackage{tikz}
\begin{document}

New solution:

\begin{tikzpicture}[yscale=1] 
\newcounter{sub}
\setcounter{sub}{1}
\draw (0, 0) grid (5, 5);  % 4x4 grid
%  for each loop
\foreach \y in {3,2,1}{
\foreach \x in {1,2,3}{
\draw [color=blue, fill=blue] (\x, \y) circle (0.1);  % for the blue cicles
\node at (\x+0.25, \y+0.25) {$P_{\thesub}$};          % for the P_sub labels
\stepcounter{sub}
}}                                                    % what follows are OP's code mainly
\node at (4,-.5) {$\frac{1}{2}$};
\node at (-.5,4) {$\frac{1}{2}$};

\node at (-0.1, -0.5) {(0, 0)};% origin label
\draw [green, thick, domain=0:4] plot ({0},\x);
\draw [green, thick, domain=0:4] plot ({4},\x);

\node at (6, 0) {x};% x-axis label
\draw [thick,->] (0, 0) -- (5.5, 0);
\node at (0, 6) {y};% y-axis label
\draw [thick,->] (0, 0) -- (0, 5.5);
\draw [color=blue, fill=black] (0, 0) circle (0.1);
\end{tikzpicture}

\end{document}