[Tex/LaTex] TikZ bounding box too big

bounding boxpgfmathtikz-pgf

I'm fairly new to LaTeX and I'm trying to make a homework template that uses figures generated in LaTeX but when I use TikZ I get a bounding box that is way too large. What is the easiest way to automatically get TikZ to make a tight bounding box?

Here is my code:

\documentclass{article}[12 pt]
\usepackage{amsmath,amssymb, amsthm, pgfplots} % Math packages
\pgfplotsset{compat=1.6}

\usepackage{tikz}

\usetikzlibrary{arrows}

\usepackage[margin=1in]{geometry} % Page settings

\usepackage{fancyhdr} %Used for headers
\usepackage{graphicx}


 \pagestyle{fancy} % Sets page style to fancy
\fancyhf{} % Clears standard header
\lhead{\textbf{Algebra II}} %Subject Name
\chead{\textbf{Name:}\hspace{3 in}}
\rhead{\textbf{Mr. A}}  %Teacher name

\rfoot{Page \thepage}


\begin{document}

\begin{flushright}
\textbf{Period:}  \makebox[0.5 in]{\hrulefill} %Transfer this to header
\textbf{Date:}  \makebox[0.75 in]{\hrulefill} \\[20 pt]
\end{flushright}

\center{\LARGE{\textbf{Intervals, Parent Functions and Domain \& Range}}}

%Create Question command

\center{Directions: Given a representation of an interval, give the other two representations. Below are examples of the three representations} \\

\begin{tikzpicture}
\begin{axis}[
axis y line=none,
axis lines=left,
axis line style={<->},
xmin=-5,
xmax=5,
ymin=0,
ymax=1,
%xlabel=$\mathbb R$,
scatter/classes={
                o={mark=*,draw=black,fill=white},
                c={mark=*,black}
                },
restrict y to domain=0:1,
xtick={-5,-4,...,5},
point meta=explicit symbolic,
 ]
 \addplot[scatter,black,ultra thick] table [y expr=0,meta index=1, header=false] {
 -2 c
 3 c
 };


 \end{axis}

 \end{tikzpicture}





 \end{document}

Best Answer

You could clip the plot although specifying the bounding box for the picture as Symbol1 suggests is probably better.

Clipping

\documentclass{article}[12 pt]
\usepackage{amsmath,amssymb, amsthm, pgfplots} % Math packages
\pgfplotsset{compat=1.6}

\usepackage{tikz}

\usetikzlibrary{backgrounds}% only to show the bounding box

\usepackage[margin=1in]{geometry} % Page settings

\usepackage{fancyhdr} %Used for headers

\pagestyle{fancy} % Sets page style to fancy
\fancyhf{} % Clears standard header
\lhead{\textbf{Algebra II}} %Subject Name
\chead{\textbf{Name:}\hspace{3 in}}
\rhead{\textbf{Mr. A}}  %Teacher name

\rfoot{Page \thepage}


\begin{document}

  \begin{flushright}
    \textbf{Period:}  \makebox[0.5 in]{\hrulefill} %Transfer this to header
    \textbf{Date:}  \makebox[0.75 in]{\hrulefill} \\[20 pt]
  \end{flushright}

  \begin{center}% center is an environment...
    \LARGE{\textbf{Intervals, Parent Functions and Domain \& Range}}

  %Create Question command

  Directions: Given a representation of an interval, give the other two representations. Below are examples of the three representations.
  \end{center}

  \begin{tikzpicture}[show background rectangle]% only to show (roughly) the bounding box
    \clip (-.5,-1) |- (7.5,1) |- cycle;
    \begin{axis}[
      axis y line=none,
      axis lines=left,
      axis line style={<->},
      xmin=-5,
      xmax=5,
      ymin=0,
      ymax=1,
      %xlabel=$\mathbb R$,
      scatter/classes={
        o={mark=*,draw=black,fill=white},
        c={mark=*,black}
      },
      restrict y to domain=0:1,
      xtick={-5,-4,...,5},
      point meta=explicit symbolic,
      ]
      \addplot[scatter,black,ultra thick] table [y expr=0,meta index=1, header=false] {
        -2 c
        3 c
      };
    \end{axis}
  \end{tikzpicture}

\end{document}

Setting bounding box

Or you can set the bounding box explicitly:

\documentclass{article}[12 pt]
\usepackage{amsmath,amssymb, amsthm, pgfplots} % Math packages
\pgfplotsset{compat=1.6}

\usepackage{tikz}

\usepackage[margin=1in]{geometry} % Page settings

\usepackage{fancyhdr} %Used for headers

\pagestyle{fancy} % Sets page style to fancy
\fancyhf{} % Clears standard header
\lhead{\textbf{Algebra II}} %Subject Name
\chead{\textbf{Name:}\hspace{3 in}}
\rhead{\textbf{Mr. A}}  %Teacher name

\rfoot{Page \thepage}


\begin{document}

  \begin{flushright}
    \textbf{Period:}  \makebox[0.5 in]{\hrulefill} %Transfer this to header
    \textbf{Date:}  \makebox[0.75 in]{\hrulefill} \\[20 pt]
  \end{flushright}

  \begin{center}% center is an environment...
    \LARGE{\textbf{Intervals, Parent Functions and Domain \& Range}}

  %Create Question command

  Directions: Given a representation of an interval, give the other two representations. Below are examples of the three representations.
  \end{center}

  \begin{tikzpicture}
    \draw [use as bounding box] (-5mm,-7.5mm) rectangle (75mm,5mm);
    \begin{axis}[
      axis y line=none,
      axis lines=left,
      axis line style={<->},
      xmin=-5,
      xmax=5,
      ymin=0,
      ymax=1,
      %xlabel=$\mathbb R$,
      scatter/classes={
        o={mark=*,draw=black,fill=white},
        c={mark=*,black}
      },
      restrict y to domain=0:1,
      xtick={-5,-4,...,5},
      point meta=explicit symbolic,
      ]
      \addplot[scatter,black,ultra thick] table [y expr=0,meta index=1, header=false] {
        -2 c
        3 c
      };
    \end{axis}
  \end{tikzpicture}

\end{document}

Saner result

Note that center is an environment and not a command.