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: END specifies the range of the variable x.
  • blue/red/brown/green/cyan/magenta/yellow/black/gray/white/darkgray/lightgray/lime/olive/orange/pink/purple/teal/violet can be mixed by COLOR1!PERCENTAGE!COLOR2.
  • xlabel/ylabel={LABEL}
  • xtick/ytick={START, START+DELTA, ..., END} separates the ticks between START and END with step length DELTA.
  • 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/none3 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 the domain.
  • smooth is an alternative to samples, 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*/cubes4
  • no marks
  • mark repeat=N marks the data points every N 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.

Footnotes:

1

For polaraxis, \usepgfplotslibrary{polar} in preamble is required.

2

Transformation from radian to degree, i.e. deg(), is needed for trigonometric functions, which accepts degree only.

3

The default value is box.

4

Usually \usetikzlibrary{plotmarks} is needed. cubes is only for 3D plots.