# Graphing with custom axes

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

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

• You can find that graphic in the tikz manual. Apr 20, 2013 at 1:46
• Welcome to TeX.sx! Apr 20, 2013 at 1:53

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}


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,
]
node[pos=1,right] {$f(x)=x$};

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

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


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,
]