Skip to content

pgfplots basics: plots in LaTeX with examples

Load pgfplots, open a tikzpicture with an axis inside, and add each data series with \addplot, either as a list of coordinates or as a formula.

The smallest plot

Every pgfplots plot has the same three layers: a tikzpicture, an axis inside it, and one \addplot per data series. Load the package with \usepackage{pgfplots} in the preamble.

Four points joined by a line

LaTeXPreamble: \usepackage{pgfplots}Open in editor (opens in a new tab)
\begin{tikzpicture}
  \begin{axis}
    \addplot coordinates {(0,1) (1,3) (2,2) (3,4)};
  \end{axis}
\end{tikzpicture}

The axis picks its own range and ticks from the data. A plain \addplot takes its colour and mark from the default cycle list: blue with round marks for the first series, red with squares for the second, and so on.

Plotting a formula

Instead of coordinates, give \addplot a formula in braces. domain sets the range of x and samples the number of points. Without them, pgfplots uses the range -5 to 5 with 25 samples.

A function with domain and samples

LaTeXPreamble: \usepackage{pgfplots}Open in editor (opens in a new tab)
\begin{tikzpicture}
  \begin{axis}
    \addplot[blue, thick, domain=-2:2, samples=60] {x^3 - 2*x};
  \end{axis}
\end{tikzpicture}

Options in square brackets after \addplot style that series. When you give options without a +, the series is drawn in black (or the colour you name) without marks; \addplot+[...] keeps the cycle list style and adds your options to it.

Labels, title, legend and grid

These options go on the axis, so they affect the whole plot.

Axis labels, a title, a legend and a grid

LaTeXPreamble: \usepackage{pgfplots}Open in editor (opens in a new tab)
\begin{tikzpicture}
  \begin{axis}[
      title={Cooling of a cup of tea},
      xlabel={Time (min)},
      ylabel={Temperature ($^\circ$C)},
      grid=major,
      legend pos=north east,
    ]
    \addplot coordinates {(0,80) (5,64) (10,53) (15,44) (20,38)};
    \addlegendentry{Measured}
    \addplot[red, domain=0:20, samples=40] {22 + 58*exp(-0.08*x)};
    \addlegendentry{Model}
  \end{axis}
\end{tikzpicture}
Rendered by Scissor LaTeX: Axis labels, a title, a legend and a grid

Labels are text, so math goes between $ ... $ as in a paragraph. \addlegendentry after each \addplot names that series; you can also list all names at the end with \legend{Measured, Model}.

Common axis options

OptionWhat it doesIn Scissor LaTeX
xlabel, ylabel, titletext along the axes and above the plotWorks
xmin, xmax, ymin, ymaxfix the visible rangeWorks
width, heightsize of the whole picture, for example width=\linewidthWorks
grid=major, grid=bothgrid lines at the major (and minor) ticksWorks
xtick={0,1,2}, xticklabels={a,b,c}choose the ticks and their labelsWorks
legend pos=north westnorth west, north east, south west, south east, outer north eastWorks
axis lines=leftan L-shaped pair of axes with arrows instead of a boxWorks (also middle, center, none)
enlargelimits=falseno extra space around the dataWorks
axis equalthe same scale on both axesNot supported yet: ignored with a message in the Problems tab

Most other options from the pgfplots manual work as well; the line plots page lists styles and marks. An option the renderer does not know is ignored, and the Problems tab names it.

Size and placement

By default a plot is about 240 by 207 points, the pgfplots default. Set width and height on the axis to change it. To give a plot a caption and a number, put the tikzpicture inside a figure, as you would an image.

A plot in a figure with a caption

LaTeXPreamble: \usepackage{pgfplots}Open in editor (opens in a new tab)
\begin{figure}[h]
  \centering
  \begin{tikzpicture}
    \begin{axis}[width=7cm, height=4.5cm, xlabel=$x$, ylabel=$x^2$]
      \addplot[domain=0:2, samples=30] {x^2};
    \end{axis}
  \end{tikzpicture}
  \caption{A parabola on $[0, 2]$.}
  \label{fig:parabola}
\end{figure}

Figure~\ref{fig:parabola} shows the parabola.
Rendered by Scissor LaTeX: A plot in a figure with a caption

\resizebox{0.5\linewidth}{!}{...} around a single tikzpicture also works: the picture is scaled to that width.

What differs from a TeX installation

Everything on this page also compiles in a TeX installation with pgfplots. The full manual is on CTAN: ctan.org/pkg/pgfplots.

Questions and answers

Do I need to install pgfplots?

No. Write \usepackage{pgfplots} as usual. Scissor LaTeX recognises the package and draws the plots itself, in the browser.

Why is my function plot jagged?

pgfplots joins 25 points by default. Add samples=100 (or more) to the \addplot or the axis for a smoother curve, or add smooth to draw a curve through the points.

Can I use pgfplots without tikzpicture?

No, in LaTeX the axis environment always sits inside a tikzpicture. Scissor LaTeX follows the same rule, so your code also compiles elsewhere.

Every example on this page is typeset by Scissor LaTeX when the site is built. Last updated 2026-11-06.