[Tex/LaTex] How to make a simple graph with grids using pgfplots

pgfmathpgfplots

I want it to look like this:

example

Preferably with comments so I can learn how to make my own!

Best Answer

This should get you started:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
  axis lines=middle,
  grid=major,
  xmin=-5,
  xmax=5,
  ymin=-3,
  ymax=5,
  xlabel=$x$,
  ylabel=$y$,
  xtick={-4,-3,...,4},
  ytick={-2,-1,...,4},
  tick style={very thick},
  legend style={
  at={(rel axis cs:0,1)},
  anchor=north west,draw=none,inner sep=0pt,fill=gray!10}
]
\addplot[blue,thick,samples=100] {x^2};
\addlegendentry{$y=x^2$}
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

The code itself is self explanatory IMO. For details, run texdoc pgfplots from command prompt/terminal and look in to the pgfplots manual.