Tx · LaTeX & TikZ

How to Set Axis Parameters in a TikZ/PGFPlots Figure

Published By the 9to5Science editorial team

At a glance

  1. 1. Load PGFPlots

    Add the pgfplots package and set a compatibility level in the document preamble.

  2. 2. Use an axis environment

    Put plot-specific options in the square brackets after \begin{axis}.

  3. 3. Set limits and labels

    Choose bounds, axis labels, ticks, grid lines, and dimensions with PGFPlots keys.

  4. 4. Add the plot

    Place one or more \addplot commands inside the axis environment.

In a PGFPlots figure, axis settings go in the optional argument of \begin{axis}[...]. That is where you set limits, labels, tick marks, grid lines, and the plot’s dimensions. The outer tikzpicture also accepts options, but those apply to the TikZ picture as a whole rather than configuring the graph’s axes.

A minimal working example

The following document draws y=x2y=x^2 with labeled axes, fixed limits, and a grid. It uses the pgfplots package and its compat setting.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    width=10cm,
    height=6cm,
    xmin=-2, xmax=2,
    ymin=0, ymax=4,
    xlabel={$x$},
    ylabel={$y$},
    title={$y=x^2$},
    grid=major,
    xtick={-2,-1,0,1,2}
  ]
    \addplot[blue, thick, domain=-2:2, samples=100] {x^2};
  \end{axis}
\end{tikzpicture}

\end{document}

The settings between the axis brackets are PGFPlots keys. width and height set the axis dimensions; xmin, xmax, ymin, and ymax set the visible coordinate range. xlabel, ylabel, and title add text, while grid=major displays the major grid lines. The xtick list specifies the x-axis tick positions. The \addplot command, inside the axis, supplies the function to draw.

Where each kind of option belongs

Use the axis options for things that describe the graph’s coordinate system or appearance: axis limits, labels, ticks, scaling, grid, and plot size. For example:

\begin{axis}[
  xmin=0, xmax=10,
  xlabel={Time (s)},
  ylabel={Distance (m)}
]

Use tikzpicture options for properties of the overall TikZ picture, such as a transformation or a style that should apply to the whole picture. For instance, \begin{tikzpicture}[scale=0.9] is a TikZ-level setting. It is not a replacement for width=... or height=... in PGFPlots. If you want to size an axis predictably, set its dimensions in the axis options.

A common source of confusion is writing xlabel or xmin directly in the tikzpicture brackets. Those are PGFPlots axis keys, so they belong in \begin{axis}[...], not \begin{tikzpicture}[...].

Frequently used axis parameters

PurposeExample keys
Axis limitsxmin=0, xmax=10, ymin=-1, ymax=1
Axis labels and titlexlabel={Time}, ylabel={Value}, title={Measurement}
Axis dimensionswidth=10cm, height=6cm
Tick positionsxtick={0,2,4,6,8,10}; similarly, ytick={-1,0,1}
Grid linesgrid=major or grid=both
Axis placementaxis lines=left

These are examples, not an exhaustive list. PGFPlots has many additional keys for logarithmic axes, legends, tick formatting, and label placement. For a key that does not seem to take effect, check that it is valid for the axis environment and that it is spelled correctly.

Common mistakes

  • Putting axis keys on the wrong environment: xmin, xlabel, and grid belong in \begin{axis}[...].
  • Leaving out the package: The axis environment and \addplot are provided by pgfplots, so load it with \usepackage{pgfplots}.
  • Expecting limits to choose the tick marks: xmin and xmax set the visible range; use xtick when you want to control tick positions explicitly.
  • Mixing up plot bounds and function domain: domain=-2:2 controls where the function is sampled. The xmin and xmax keys control the displayed axis range. They often match, but they serve different purposes.
  • Scaling the whole picture to set the graph dimensions: Prefer width and height in the axis options when you need a specific plot size.

For many figures, the practical pattern is simple: open a tikzpicture, put axis-related keys in \begin{axis}[...], add plots inside the axis, and close both environments.

References

  1. PGFPlots — Manual for Package pgfplots
  2. TikZ and PGF Manual

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.