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.

How would one go about creating a graph in LaTeX with custom axes, colors and labels, something like this: enter image description here

If anyone knows the exact code to create this, that would be extremely helpful, but if not, don't fret it!

share|improve this question
4  
You can find that graphic in the tikz manual. –  Sigur Apr 20 '13 at 1:46
1  
Welcome to TeX.sx! –  Kurt Apr 20 '13 at 1:53

1 Answer 1

up vote 7 down vote accepted

As Sigur said in his comment, your graphic is taken from the TikZ manual:

\documentclass{standalone}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[domain=0:4]
  \draw[very thin,color=gray] (-0.1,-1.1) grid (3.9,3.9);
  \draw[->] (-0.2,0) -- (4.2,0) node[right] {$x$};
  \draw[->] (0,-1.2) -- (0,4.2) node[above] {$f(x)$};
  \draw[color=red]    plot[id=x]   function{x}           node[right] {$f(x) =x$};
  \draw[color=blue]   plot[id=sin] function{sin(x)}       node[right] {$f(x) = \sin x$};
  \draw[color=orange] plot[id=exp] function{0.05*exp(x)} node[right] {$f(x) = \frac{1}{20} \mathrm e^x$};
\end{tikzpicture}

\end{document}

enter image description here

The example can be translated via pdflatex -shell-escape file.tex (the -shell-escape is necessary because plot function calls gnuplot).


However, "Graphing with custom axes" might be beyond this example. In particular, "custom axes" might involve automatically computed limits, scientific data range, automatically plotted axes along with suitable ticks, and perhaps more. There is the plotting package pgfplots which is built on top of tikz which can do it. Using pgfplots, it would become

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.8}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        domain=0:4,
        axis lines=center,
        enlargelimits=upper,
        grid=major,
        xlabel=$x$,
        ylabel=$f(x)$,
        % 
        % disable clipping at axis box to show nodes:
        clip=false,
    ]
    \addplot[red] {x}   
        node[pos=1,right] {$f(x)=x$};

    \addplot[orange] {1/20 * exp(x)} 
        node[pos=1,right] {$f(x)=\frac{1}{20} \mathrm e^x$};

    \addplot[blue] {sin(deg(x))}
        node[pos=1,right] {$f(x)=\sin(x)$};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Note that it needs only pdflatex file.tex. The differences are that the plain tikz example issues explicit drawing commands for the axis and the grid. Furthermore, there is no distinction between tick marks and grid lines and there are no tick labels (which would require two \foreach loop). The pgfplots example needs to be fine-tuned because the default configuration of pgfplots is a boxed axis. In addition, pgfplots typically generates legends using the legend entries option so we have to add clip=false to allow the tikz-style legend to be large than the axis.


Here would be the simpler style in which legends cannot overlap using pgfplots (and the default pgfplots styles):

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.8}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        domain=0:4,
        grid=major,
        xlabel=$x$,
        ylabel=$f(x)$,
        legend entries={
            $f(x) = x$,
            $f(x)=\frac{1}{20} \mathrm e^x$,
            $f(x)=\sin(x)$
        },
        legend pos=outer north east,
    ]
    \addplot[red] {x};

    \addplot[orange] {1/20 * exp(x)};

    \addplot[blue] {sin(deg(x))};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
    
Extremely comprehensive! Thank you! –  KevinOrr Apr 21 '13 at 2:58

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.