LaTeX - Pgfplots
Table of Contents
Introduction
Pgfplots is an excellent LaTeX package to plot high quality graphs. This post just summaries its basic usages.
Installation
Pgfplots is a built-in package in the texlive. As long as texlive is ready, pgfplots is out-of-the-box.
Usage
Pgfplots depends on pgf/tikz, and the statements are encapsulated in the environment of tikzpicture. The basic plot commands are \addplot
and \addplot3
for 2D or 3D plot purpose, respectively. It should be noticed that each of them has a variant, i.e. \addplot+
and \addplot3+
. \addplot/\addplot3
uses the provided local options only, while \addplot+/\addplot3+
adopts both the global and local options.
Templates for pgfplots usage can be presented as follows.
\usepackage{pgfplots} \pgfplotsset{GLOBAL-OPTIONS} ... % For single curve plot \begin{tikzpicture} \begin{AXIS-TYPE}[AXIS-OPTIONS] \addplot/\addplot3[PLOT-OPTIONS] {FORMULA}; \legend{LABEL}; \end{AXIS-TYPE} \end{tikzpicture} % For multi-curve plot \begin{tikzpicture} \begin{AXIS-TYPE}[AXIS-OPTIONS] \addplot/\addplot3[PLOT-OPTIONS] {FORMULA1}; \addlegendentry{LABEL1}; \node at (AXIS-TYPE cs:x1,y1) {TEXT}; % Annotation for the curve. \addplot/\addplot3[PLOT-OPTIONS] {FORMULA2}; \addlegendentry{LABEL2}; ... \end{AXIS-TYPE} \end{tikzpicture}
AXIS-TYPE
The candidate values are axis/semilogxaxis/semilogyaxis/loglogaxis/polaraxis
1.
FORMULA
- plain function2, e.g.
\addplot {f(x)};
- parameter function, e.g.
\addplot ({x(t)}, {y(t)});
- coordinates, e.g.
\addplot coordinates{(x1, y1)(x2, y2)...(xn, yn)}
- data in a file, e.g.
\addplot table [x = COLUMN1, y = COLUMN2]{DATA-FILE};
with the content of DATA-FILE
as below.
COLUMN1 COLUMN2 COLUMN3 a1 b1 c1 a2 b2 c2 ... ... ...
- shade a region, e.g.
\addplot [...] {...} \closedcycle;
Options
AXIS-OPTIONS
and PLOT-OPTIONS
are basically the same, and the latter overrides the former.
title={TITLE}
domain=START: END
specifies the range of the variablex
.blue/red/brown/green/cyan/magenta/yellow/black/gray/white/darkgray/lightgray/lime/olive/orange/pink/purple/teal/violet
can be mixed byCOLOR1!PERCENTAGE!COLOR2
.xlabel/ylabel={LABEL}
xtick/ytick={START, START+DELTA, ..., END}
separates the ticks betweenSTART
andEND
with step lengthDELTA
.xticklabels/yticklabels={a, b, c, ...}
axis equal image
equally scales the axes, and trims the width or height to suit.axis lines=box/left/middle/center/right/none
3 specified the way the axes are drawn, e.g.middle
make the axes intersect at the origin.grid
grid style=solid/dashed
xmajorgrids/ymajorgrids=true/false
enables/disables grid lines at the tick positions on the x/y axis.legend pos=outer north east/north west/north east/south west/south east
legend cell align=left/right
xmin/xmax/ymin/ymax=BOUND
data cs=cart/polar
specifies the coordinate, e.g.(x, y)
or(angle, radius)
.line width=WIDTH
, e.g.line width=1pt
.thin/ultra thin/very thin/thick/semithick/ultra thick/very thick
solid/dashed/dotted/dashdotted/dashdotdotted
- customize font size
tick label style={font=\normalsize\small\footnotesize\tiny} label style={font=\normalsize\small\footnotesize\tiny} legend style={font=\normalsize\small\footnotesize\tiny}
samples=SAMPLE_NUMBER
defines the number of points in thedomain
.smooth
is an alternative tosamples
, which makes a smooth curves between data points.mark=*/x/+/|/o/asterisk/star/10-pointed star/oplus/oplus*/otimes/otimes*/square/square*/triangle/triangle*/diamond/halfdiamond*/halfsquare*/right*/left*/halfcircle/halfcircle*/pentagon/pentagon*/cubes
4no marks
mark repeat=N
marks the data points everyN
ones instead of every one.only marks
hides the line joining the data points.opacity=FRACTION
makes something not transparent, e.g.fill opacity=0.6
.xbar/ybar
for bar plot.