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/polaraxis1.
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: ENDspecifies the range of the variablex.blue/red/brown/green/cyan/magenta/yellow/black/gray/white/darkgray/lightgray/lime/olive/orange/pink/purple/teal/violetcan be mixed byCOLOR1!PERCENTAGE!COLOR2.xlabel/ylabel={LABEL}xtick/ytick={START, START+DELTA, ..., END}separates the ticks betweenSTARTandENDwith step lengthDELTA.xticklabels/yticklabels={a, b, c, ...}axis equal imageequally scales the axes, and trims the width or height to suit.axis lines=box/left/middle/center/right/none3 specified the way the axes are drawn, e.g.middlemake the axes intersect at the origin.gridgrid style=solid/dashedxmajorgrids/ymajorgrids=true/falseenables/disables grid lines at the tick positions on the x/y axis.legend pos=outer north east/north west/north east/south west/south eastlegend cell align=left/rightxmin/xmax/ymin/ymax=BOUNDdata cs=cart/polarspecifies 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 thicksolid/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_NUMBERdefines the number of points in thedomain.smoothis 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*/cubes4no marksmark repeat=Nmarks the data points everyNones instead of every one.only markshides the line joining the data points.opacity=FRACTIONmakes something not transparent, e.g.fill opacity=0.6.xbar/ybarfor bar plot.