Graphing Functions
To graph functions there are two options for packages: tikz
and pgfplots
. TikZ is familiar if you regularly use it for drawing diagrams, but PGFPlots is a lot more powerful. Below is an example of using each package.
- TikZ
- PGFPlots
\documentclass[margin=1mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[->] (-3,0) -- (3,0) node[above] {$x$};
\draw[->] (0,-3) -- (0,3) node[right] {$y$};
\draw[<->,
domain=-1:3,
smooth,
variable=\x
] plot ({\x}, {\x*\x-2*\x});
\end{tikzpicture}
\end{document}
Produces the following:
\documentclass[margin=1mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=center,
xmin=-3,
xmax=3,
ymin=-3,
ymax=3,
xlabel={$x$},
ylabel={$y$},
ticks=none,
]
\addplot[
<->,
domain=-.97:2.97,
smooth,
] {x^2-2*x};
\end{axis}
\end{tikzpicture}
\end{document}
Produces the following:
From here on I will be focusing on drawing graphs with PGFPlots. If you're interested in learning how to draw using TikZ, see the documentation.
Configuring the axes
By default PGFPlots draws its axis around the edge of the graph like so:
\documentclass[margin=1mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot[
<->,
domain=-2:4,
smooth,
] {x^2-2*x};
\end{axis}
\end{tikzpicture}
\end{document}
To draw the axes so they pass through the origin, add axis lines=center
to the options for the axis. For example:
\documentclass[margin=1mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=center
]
\addplot[
<->,
domain=-2:4,
smooth,
] {x^2-2*x};
\end{axis}
\end{tikzpicture}
\end{document}
If you look closely this has caused a few problems:
- the arrowheads of the function can get cut off.
- there is a tick drawn on the same point as the arrowhead for each axis.
- the parabola's turning point is being cut off very slightly
In my opinion, the cleanest fix for these issues is to adjust axes to be slightly larger than the range and domain of the function being drawn. This can be done by changing the ymin
, xmin
, ymax
, and xmax
in the axis options. Observe:
\documentclass[margin=1mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=center,
ymin=-1.5,
xmin=-2.25,
ymax=8.5,
xmax=4.25,
]
\addplot[
<->,
domain=-2:4,
smooth,
] {x^2-2*x};
\end{axis}
\end{tikzpicture}
\end{document}
Other useful options for the axes are:
axis line style
changes the style of the axis line e.g.axis line style=thick
x
changes the length of e.g.x=.5cm
y
similar tox
xlabel
adds a label to the -axis e.g.xlabel=$x$
ylabel
similar toxlabel
xtick
specifies where to place ticks on axis e.g.xtick={0,pi/2,pi,3*pi/2,2*pi}
ytick
similar toxtick
xticklabels
specifies what labels to use for eachxtick
e.g.xticklabels = {$\frac{\pi}{2}$, $\pi$, $\frac{3\pi}{2}$, $2\pi$}
yticklabels
similar toxticklabels
xtick distance
changes the frequency of ticks along the -axis e.g.xtick distance = .5
ytick distance
similar toxtick distance
grid
enables grid lines, eithermajor
,minor
orboth
major grid style
changes the style of the major grid lines e.g.major grid style = {dashed, blue}
minor grid style
similar tomajor grid style
width
specifies the width of the graph e.g.width=10cm
height
similar towidth
Drawing functions
To draw functions you addplot
s to your axis. Functions must be written within curly brackets after \addplot
. Functions you can draw include:
\addplot {x^2}
\addplot {2*x-1}
\addplot {2^x}
\addplot {sin(\x)}
sine wave in degrees\addplot {sin(\x r)}
sine wave in radians
addplot
also has options you can use including:
<->
to add arrowheads on both endsthick
orline width=size
(size
can be2pt
,.5pt
etc.) to adjust the thickness of the linedomain=min:max
to adjust the domainsmooth
to smooth out a curvesamples=n
(wheren
the number of samples) to change the number of points that are calculated for plotting the function.
Important
When a function has asymptotes the graph can play up a bit. I would suggest adding multiple plots and adjust the domains to avoid drawing any asymptotes.
For example, graphing :
\documentclass[margin=1mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = center,
axis line style = thick,
xlabel = $x$,
ylabel = $f(x)$,
xmin = 0, xmax = 6.5,
ymin = -6.5, ymax = 6.5,
xtick = {pi/2, pi, 3*pi/2, 2*pi},
xticklabels = {$\frac{\pi}{2}$, $\pi$, $\frac{3\pi}{2}$, $2\pi$},
]
\addplot[
domain = 0:pi/2-.1,
samples = 200,
] {tan(\x r)};
\addplot[
domain = pi/2+.1:3*pi/2-.1,
samples = 200,
] {tan(\x r)};
\addplot[
domain = 3*pi/2+.1:2*pi,
samples = 200,
] {tan(\x r)};
\end{axis}
\end{tikzpicture}
\end{document}
Notice there are three plots, and the domains have been made to end and start on either side of the asymptotes.
Drawing asymptotes
\addplot
also be used for drawing lines between two points which can be styled to represent an asymptote.
To draw a line between points the syntax is as follows:
\addplot[options] coordinates {(x1,y1)(x2,y2)};
As demonstrated below:
\documentclass[margin=1mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = center,
axis line style = thick,
xlabel = $x$,
ylabel = $f(x)$,
xmin = 0, xmax = 6.5,
ymin = -6.5, ymax = 6.5,
xtick = {pi/2, pi, 3*pi/2, 2*pi},
xticklabels = {$\frac{\pi}{2}$, $\pi$, $\frac{3\pi}{2}$, $2\pi$},
]
\addplot[
domain = 0:pi/2-.1,
samples = 200,
] {tan(\x r)};
\addplot[
domain = pi/2+.1:3*pi/2-.1,
samples = 200,
] {tan(\x r)};
\addplot[
domain = 3*pi/2+.1:2*pi,
samples = 200,
] {tan(\x r)};
\addplot[
dashed,
samples=50,
] coordinates {(pi/2,-6.5)(pi/2,6.5)};
\addplot[
dashed,
samples=50,
] coordinates {(3*pi/2,-6.5)(3*pi/2,6.5)};
\end{axis}
\end{tikzpicture}
\end{document}
Shading the area between curves
To shade the area between curves the first thing to do is to add a second plot and name each of them. In the example below I have named them curve1 and curve2 respectively.
\documentclass[margin=1mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = center,
axis line style = thick,
xlabel = $x$,
ylabel = $y$,
ymax = 7.5,
ymin = -0.0075,
xmax = 3.5,
xmin = -0.0075,
ytick distance = 1,
xtick distance = 1,
]
\addplot[
name path = curve1,
->,
domain = 0:3.3,
samples = 200,
] {x};
\addplot[
name path = curve2,
->,
domain = 0:pi,
samples = 200,
] {sin(\x r)*e^x};
\end{axis}
\end{tikzpicture}
\end{document}
Next we will add a plot which fills between curve1 and curve2. To do this we need to add the fillbetween
PGFPlots library to our preamble by adding \usepgfplotslibrary{fillbetween}
.
Note you can specify the domain you want highlighted using soft clip = {domain = min:max}
.
\documentclass[margin=1mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = center,
axis line style = thick,
xlabel = $x$,
ylabel = $y$,
ymax = 7.5,
ymin = -0.0075,
xmax = 3.5,
xmin = -0.0075,
ytick distance = 1,
xtick distance = 1,
]
\addplot[
name path = curve1,
->,
domain = 0:3.3,
samples = 200,
] {x};
\addplot[
name path = curve2,
->,
domain = 0:pi,
samples = 200,
] {sin(\x r)*e^x};
\addplot[
fill = black,
opacity = 0.25,
]
fill between [
of = curve1 and curve2,
soft clip = {
domain = 0:2.99
}
];
\end{axis}
\end{tikzpicture}
\end{document}
Finally, you may also want to shade the area between a curve and the -axis. We can do this by adding a plot along the -axis which we already know how to do:
\documentclass[margin=1mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = center,
axis line style = thick,
xlabel = $x$,
ylabel = $f(x)$,
xmin = -1.5, xmax = 3.5,
ymin = 0, ymax = 21.5,
ytick distance = 5,
y = 5,
]
\addplot[
name path = f(x),
<->,
domain = -1.2:3,
samples = 200,
] {e^x};
\addplot[
name path = xaxis,
] coordinates {(-1,0)(3,0)};
\addplot[
fill = black,
opacity = 0.125,
]
fill between [
of = f(x) and xaxis,
soft clip={
domain=1:2
}
];
\end{axis}
\end{tikzpicture}
\end{document}
Marking Points
TikZ and PGFPlots can be combined. You are able to add points to a graph using \node
from TikZ. The syntax is as follows:
\node[options] at (x,y) {label};
Note that when using nodes to mark a point you will most commonly be adding a label into the options, and leaving the curly braces empty. The label inside the curly braces is if you want to write inside the node. The most common options for marking and labelling a point using a TikZ node is as follows:
\node[
label={angle:{label}},
circle,
fill = black,
inner sep=1pt
]
Where:
angle
is the number of degrees from the -axis. (think unit circle).label
is your label. Note you can use$
$
to type in mathematics.circle
is the shape of the markingfill
changes the colourinner sep
indicates the separation of the text inside of the node to boundary. Note unless there is a label inside the curly braces this will only change the size of the marking.
For example,
\documentclass[margin=1mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = center,
axis line style = thick,
xlabel = $x$,
ylabel = $f(x)$,
xmin = -1.5, xmax = 3.5,
ymin = 0, ymax = 21.5,
ytick distance = 5,
y = 5,
]
\addplot[
name path = f(x),
<->,
domain = -1.2:3,
samples = 200,
] {e^x};
\addplot[
name path = xaxis,
] coordinates {(-1,0)(3,0)};
\addplot[
fill = black,
opacity = 0.125,
]
fill between [
of = f(x) and xaxis,
soft clip={
domain=1:2
}
];
\node[
label={90:{$(1,e)$}},
circle,
fill = black,
inner sep=1pt
] at (1,e^1) {};
\end{axis}
\end{tikzpicture}
\end{document}