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!
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityHow 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!
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,
]
\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}
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}