# Extend axis outside boxed area in pgfplots

I prefer the look of the second graph but want the functionality of the first. That is, define the equation by its name rather then transforming points.

How can I extend the axis outside the boxed area in the first graph to look more like the second graph.

\documentclass[12pt,addpoints]{exam}
\usepackage{pgfplots}
\usetikzlibrary{backgrounds}
\pgfplotsset{every axis/.append style={
axis x line=middle,
axis y line=middle,
axis line style={<->},
xlabel={$x$},
ylabel={$y$},
line width=1pt,}}

% line style
\pgfplotsset{cmhplot/.style={color=black,mark=none,<->}}

% arrow style
\tikzset{>=stealth}

% framing the graph
\tikzset{tight background}

\begin{document}

\begin{tikzpicture}
\begin{axis}[framed,
xmin=-10,xmax=10,
ymin=-10,ymax=10,
xtick={-8,-6,...,8},
xticklabels={,,,,,,,,},
ytick={-8,-6,...,8},
yticklabels={,,,,,,,,},
grid=both]
\end{axis}
\end{tikzpicture} \\

\begin{tikzpicture}[scale=.3]
\begin{scope}
\clip (-10,-10) rectangle (10,10);
\draw[step=2cm,gray,very thin]
(-12,-12) grid (10,10);
\end{scope}
\draw [<->] (-11,0) -- (11,0);
\draw [<->](0,-11) -- (0,11);
%\clip (-10,-10) rectangle (10,10);
\end{tikzpicture}

\end{document}

-
Your mwe doesn't compile and gives a few errors. – percusse Aug 28 '12 at 18:07

## Extend Axis Past Grid:

I am not sure if there is a pre defined way to have different limits on the grid, but you could certainly add a grid separately as desired. For instance, with

\draw [gray, ultra thin]%
(axis cs: -8,-8) grid [step=10] (axis cs: 8,8);%


you get:

## Code:

\documentclass[12pt,addpoints]{exam}
\usepackage{pgfplots}
\usetikzlibrary{backgrounds}
\pgfplotsset{every axis/.append style={
axis x line=middle,
axis y line=middle,
axis line style={<->},
xlabel={$x$},
ylabel={$y$},
line width=1pt,}}

% line style
\pgfplotsset{cmhplot/.style={color=black,mark=none,<->}}

% arrow style
\tikzset{>=stealth}

% framing the graph
\tikzset{tight background}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
%framed,
xmin=-10,xmax=10,
ymin=-10,ymax=10,
xtick={-8,-6,...,8},
xticklabels={,,,,,,,,},
ytick={-8,-6,...,8},
yticklabels={,,,,,,,,},
%grid=minor
]
\draw [gray, ultra thin]%
(axis cs: -8,-8) grid [step=10] (axis cs: 8,8);%
\addplot[cmhplot, blue, ultra thick]expression[domain=-8.5:8.5,samples=50]{x};
\end{axis}
\end{tikzpicture}
\end{document}


## Extend Graph Past Grid:

To extend the graph beyond the grid you can limit the the min/max x and y values to be less than your graph and add the option clip=false:

## Code:

\documentclass[12pt,addpoints]{exam}
\usepackage{pgfplots}
\usetikzlibrary{backgrounds}
\pgfplotsset{every axis/.append style={
axis x line=middle,
axis y line=middle,
axis line style={<->},
xlabel={$x$},
ylabel={$y$},
line width=1pt,}}

% line style
\pgfplotsset{cmhplot/.style={color=black,mark=none,<->}}

% arrow style
\tikzset{>=stealth}

% framing the graph
\tikzset{tight background}

\begin{document}

\begin{tikzpicture}
\begin{axis}[framed,
clip=false,
xmin=-8,xmax=8,
ymin=-8,ymax=8,
xtick={-8,-6,...,8},
xticklabels={,,,,,,,,},
ytick={-8,-6,...,8},
yticklabels={,,,,,,,,},
grid=both,
]
\addplot[cmhplot, blue, ultra thick]expression[domain=-9.5:9.5,samples=50]{x};
\end{axis}
\end{tikzpicture}
\end{document}

-
Peter, thank you for the quick response. It is however the axis and not the plot that I want have extend beyond the grid (as it is in the second axis). I need the functionality of the following code: \addplot[cmhplot]expression[domain=-9.5:9.5,samples=50]{x}; and as a result need to find a way to edit the first grid rather than use the second. – Garth Fleming Sep 6 '12 at 1:08
@GarthFleming: Sorry, I misunderstood the request. Have updated the answer to also provide a method to have the axis extend past the grid. – Peter Grill Sep 6 '12 at 1:27
Thank you so so very much. This is fantastic. – Garth Fleming Sep 6 '12 at 1:33