Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
3  
Your mwe doesn't compile and gives a few errors. –  percusse Aug 28 '12 at 18:07

1 Answer 1

up vote 5 down vote accepted

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}
share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.