[Tex/LaTex] Extend axis outside boxed area in pgfplots

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]
    \addplot[cmhplot]expression[domain=-9.5:9.5,samples=50]{x};
\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}

Best Answer

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:

enter image description here

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:

enter image description here

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}
Related Question