I define a function \piecewise
to be used inside a tikz picture that takes as input a comma-separated list, with each entry having the following form:
{function} / left-endpoint / right-endpoint / {open-points} / {closed-points}
The code
\begin{tikzpicture}
\draw[->] (-3, 0) -- (3, 0) node[right] {$x$};
\draw[->] (0, -1) -- (0, 3) node[above] {$y$};
\begin{scope}[line width=1pt, blue]
\piecewise{{\x+3}/-3/-1/{-1}/{},{\x*\x}/-1/1/{}/{-1},{.5*\x+.5}/1/3/{}/{}}
\end{scope}
\end{tikzpicture}
produces the following:

The piecewise function is x+3 on the interval [-3,-1), x^2 on the interval [-1,1] and (x+1)/2 on the interval (1,3]. Note that functions must be entered to be parsed by \tikz
, so the variable x must have a backslash in the formula.
{open-points}
is a comma separated list of x-values where you want an open circle. Similarly, {closed-points}
produces filled-in circles. These can be empty lists.
If you want the axes visible inside the open circles, plot them after the function:

\begin{tikzpicture}
\begin{scope}[line width=1pt]
\piecewise{{-1}/-3/0/{0}/{},{0}/0/0/{}/{0},{1}/0/3/{0}/{}}
\end{scope}
\draw[->] (-3, 0) -- (3, 0) node[right] {$x$};
\draw[->] (0, -2) -- (0, 2) node[above] {$y$};
\end{tikzpicture}
Here is the complete code. Of course you can adjust the size of the circles (or any other aspect of the plot) to your liking.
\documentclass{article}
\usepackage{tikz}
\newcommand{\piecewise}[1]{
\foreach \f/\a/\b/\open/\closed in {#1}{%
\draw[domain=\a:\b, smooth, variable=\x] plot ({\x}, \f);
\foreach \x[evaluate={\y=\f;}] in \open{%
\draw[fill=white] (\x,\y) circle (.8mm);
}
\foreach \x[evaluate={\y=\f;}] in \closed{%
\draw[fill] (\x,\y) circle (.8mm);
}
}
}
\begin{document}
\begin{tikzpicture}
\draw[->] (-3, 0) -- (3, 0) node[right] {$x$};
\draw[->] (0, -1) -- (0, 3) node[above] {$y$};
\begin{scope}[line width=1pt, blue]
\piecewise{{\x+3}/-3/-1/{-1}/{},{\x*\x}/-1/1/{}/{-1},{.5*\x+.5}/1/3/{}/{}}
\end{scope}
\end{tikzpicture}
\vspace{2cm}
\begin{tikzpicture}
\begin{scope}[line width=1pt]
\piecewise{{-1}/-3/0/{0}/{},{0}/0/0/{}/{0},{1}/0/3/{0}/{}}
\end{scope}
\draw[->] (-3, 0) -- (3, 0) node[right] {$x$};
\draw[->] (0, -2) -- (0, 2) node[above] {$y$};
\end{tikzpicture}
\end{document}
Your example has an asymptote, which needs a little care:

I just picked .13
for the left endpoint in the first piece of the function since it looked good to me.
\begin{tikzpicture}[scale=.7]
\begin{scope}[line width=1pt]
\piecewise{{1/\x+2}/.13/1/{1}/{},{\x*\x+1}/1/2/{}/{1},{5}/2/2/{}/{2},{2*\x+1}/2/4/{}/{4},{-\x+5}/4/6.2/{4}/{}}
\end{scope}
\draw[thick,->] (-1, 0) -- (7, 0) node[right] {$x$};
\draw[thick,->] (0, -1.2) -- (0, 10) node[above] {$y$};
\node[below left] at (0,0) {0};
\draw[ultra thin] (-.4,-1.1) grid (6.2,9.8);
\end{tikzpicture}
One could also use the command to create graphs of functions with removable singularities:

\begin{tikzpicture}
\begin{scope}[line width=1pt]
\piecewise{{1}/-3/3/{0}/{}}
\end{scope}
\draw[->] (-3, 0) -- (3, 0) node[right] {$x$};
\draw[->] (0, -1) -- (0, 3) node[above] {$y$};
\node[above] at (1.5,1) {$f(x)=\frac{x}{x}$};
\node[below left] at (0,0) {0};
\node[below left] at (0,1) {1};
\end{tikzpicture}
As a side note, I strongly recommend using cases
instead of array
for formatting the function in your document.