12.5 ⁑Plots
If you have a large amount of data, you may want to consider using a mathematical tool (such as Matlab, Octave or GnuPlot) to generate your graphs as image files. If you use TeX, you may have excessively long document build times. Don't expect TeX to be able to compute logarithms or exponentials with the speed or precision of a custom-built numerical computing engine. Additionally, some packages can't parse scientific notation.
There are, however, some packages that use PostScript rather than TeX to perform the calculations (in which case you can't directly use PDFLaTeX) and some of them rely on TeX's shell escape to call an application, such as GnuPlot, to generate the drawing code (in which case you need the shell escape enabled). If you have applications such as GnuPlot or Matlab installed or if you are happy to use the latex+dvips+ps2pdf route to generate your PDF files, there are a number of useful packages listed on the graphics-plot topic. In addition, pgf/ tikz comes with an impressive mathematical engine provided by the pgfmath package. If you want to annotate your plot with text that matches your document format and have the patience to wait for your document to compile, you may be surprised by the images that can be produced using some of the packages available on CTAN.
Since the aim of this book is to be as useful to as many readers as possible, I'm again going to choose device-independent options that don't require any additional applications. (This is, after all, a book on administrative work not high performance computing or advanced mathematics.) As with pie charts and bar charts, the datatool package also provides a package (dataplot) for drawing plots from data stored in a database. However, if you don't intend using that data anywhere else in the document, I suggest you use a more flexible package such as pgfplots, which is described below. The other advantage of pgfplots over dataplot is that pgfplots can parse scientific notation and can cope with larger values. The remainder of this section discusses the pgfplots package.
The pgfplots package [26] uses pgf/tikz so make sure you have an up-to-date version of the pgf package installed. The pgfplots manual [26] is 500 pages long at the time of writing, so this section is a very brief introduction. See the user manual for further details.
Options can be set using
where ⟨options⟩ is a key=value list of options. The
pgfplots manual recommends setting the
compat
key to benefit from recent features
and to avoid possible changes if you recompile your document at a
later date with a newer version. The log file will provide
a suggested value.
Other common options include:
width
- Sets the width of the final
picture. An empty values indicates the default width or rescale in
proportion to the height (so that the aspect ratio is maintained
if you have set the height).
height
- As above but for the height.
scale only axis
- This is
a boolean key. If true, the above
width
orheight
settings apply to the size of the axes rather than the overall picture size (which may include axis labels or tick marks). xlabel
- The x-axis label.
ylabel
- The y-axis label.
title
- The plot title.
major tick length
- The length of
major tick marks.
minor tick length
- The length of
minor tick marks.
tick align
- The value may be one of:
inside
(default),outside
orcenter
. This indicates the location of the tick marks relative to the axis line.
There are many different types of plots and axes, but this
introduction will just consider normal two-dimensional Cartesian
plots, which can be produced using the axis
environment:
This should be placed inside a tikzpicture
environment.
The contents of the axis
environment may contain one or
more instance of
There are a number of different ways of specifying the plot, such as:
where ⟨co-ordinate list⟩ is a space-separated list of
co-ordinates. A standard Cartesian co-ordinate can be specified in
the form (⟨x⟩,⟨y⟩)
, for example, (0.5,3.1)
.
The ⟨trailing path specs⟩ are optional and are as the path
specifications used by tikz's \path
and \draw
commands.
where ⟨file or inline table⟩ is either a filename or the data arranged in the tabulated form of a space-separated data file. The ⟨column selection⟩ may contain the keys x=⟨column key⟩, to indicate the column containing the x values, and y=⟨column key⟩, to indicate the column containing the y values. The table data should include a header row at the start that may be used to reference the columns. If there is no header row, you can use header=false within ⟨column selection⟩ and use x index=⟨index⟩ and y index=⟨index⟩ instead of x and y to reference the required columns. The ⟨index⟩ should be an integer starting from 0 (the first column).
There are other \addplot
specifications for evaluating
expressions or using TeX's shell escape mechanism. See the
pgfplots manual [26] for further details.
The ⟨plot options⟩ can be any of the path drawing options
that are accepted by \path
(or \draw
), such as:
mark
- Sets the marker style. There
are three standard markers:
*
(filled circle),x
(cross) and+
(plus). Additional markers are available with tikz'splotmarks
library (which can be loaded using\usetikzlibrary
). no markers
- Overrides any
mark
value. mark repeat
- The value of this key should
be an integer ⟨n⟩, to indicate that only every ⟨n⟩th mark
should be drawn.
mark size
- The size of the markers.
dashed
- Dashed line style.
dotted
- Dotted line style.
dashdotted
- Dash-dot line style.
thin
- Thin line width.
thick
- Thick line width.
color
- The value should a colour name used for stroking and filling. You can omit the color= and just write the colour name.
Suppose instead of the bar chart in Exercise 32 I want a graph of company profits where the x-axis represents the year and the y-axis represents the profit. I could have a file containing that data called, say, profits.dat that contains:
year profit 2010 52000 2011 50000 2012 75000 2013 60000In which case, I can plot the data using:
or I can just have the data inline as in the example document below:
\documentclass{article} \usepackage{pgfplots} \begin{document} \begin{tikzpicture} \begin{axis} \addplot table { year profit 2010 52000 2011 50000 2012 75000 2013 60000 }; \end{axis} \end{tikzpicture} \end{document}
This produces the following message in the transcript:
Package pgfplots Warning: running in backwards compatibility mode (unsuitable tick labels; missing features). Consider writing \pgfplotsset{compat=1.11} into your preamble.So I need to add
\pgfplotsset{compat=1.11}
to the preamble. This produces the graph shown in Figure 12.12, which doesn't look right. The x-axis is far too cramped and doesn't need so many tick marks.
The positions of the x-axis tick marks can be changed
using the xtick
key. This may be set to
\empty
(generate automatically), the keyword data (use
the co-ordinates provided by the first plot), or a comma-separated list of
co-ordinates. (There are also analogous ytick
and
ztick
keys.)
The tick labels can be changed using various keys. First is the
xticklabels
key which takes
a comma-separated list of labels, where each label corresponds to a tick position.
Alternatively, you can use xticklabel
where
the value is the code to produce the label. You can access
information about the current tick using the following:
This is the current element of the tick option.
This command is only valid if the
x tick label as interval
option has been set
(or y tick label as interval
for the
y-axis ticks) in which case it will contain the position
of the next tick position.
The default label format is
\pgfmathprintnumber
{
, which uses the
number pretty-printing command provided by the pgfmath package.
This is why the x-tick labels in
Figure 12.12 have commas in them (for example, 2,010
rather than 2010). The comma can be removed by first using:
\tick
}
Alternatively, if you just want to change the style for a particular axis:
The modified document is shown below. I've also added a plot title
using title
and axes labels
using xlabel
and
ylabel
:
\documentclass{article} \usepackage{pgfplots} \pgfplotsset{compat=1.11} \begin{document} \begin{tikzpicture} \begin{axis}[xtick=data,% get x tick marks from data xlabel=Year,ylabel={Profits (\pounds)},% axes labels x tick label style={% change x tick label style /pgf/number format/set thousands separator={}% }, title={Profits Since 2010},% plot title tick align=outside% ticks on the outside ] \addplot table { year profit 2010 52000 2011 50000 2012 75000 2013 60000 }; \end{axis} \end{tikzpicture} \end{document}
This produces the plot shown in Figure 12.13, which is much less cramped.
In order to save space, the y-tick labels have been
scaled by 104 but since, in this case, the
y axis represents profits it would look better if this
scaling wasn't applied. There are a number of different ways of
changing the scaling (see the manual [26]) but to switch
it off for all axes, you just need the option
scaled ticks
=false
. It's also
possible that you don't like the boxed axes, but this can be
changed using the option axis lines*
=left
.
These extra options produce the plot shown in Figure 12.14.
You can download
or
view
this example where I have
additionally loaded the plotmarks
library and used the
optional argument of \addplot
to set the plot marks to filled
diamonds (mark=diamond*) with the marker size set to
5 pt (mark size=5pt) and a thick cyan line
stroke. (Try it for yourself before you download the example, as an
additional exercise.)
This book is also available as A4 PDF or 12.8cm x 9.6cm PDF or paperback (ISBN 978-1-909440-07-4).