How to Set Axis Parameters in a TikZ/PGFPlots Figure
At a glance
- 1. Load PGFPlots
Add the pgfplots package and set a compatibility level in the document preamble.
- 2. Use an axis environment
Put plot-specific options in the square brackets after \begin{axis}.
- 3. Set limits and labels
Choose bounds, axis labels, ticks, grid lines, and dimensions with PGFPlots keys.
- 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 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
| Purpose | Example keys |
|---|---|
| Axis limits | xmin=0, xmax=10, ymin=-1, ymax=1 |
| Axis labels and title | xlabel={Time}, ylabel={Value}, title={Measurement} |
| Axis dimensions | width=10cm, height=6cm |
| Tick positions | xtick={0,2,4,6,8,10}; similarly, ytick={-1,0,1} |
| Grid lines | grid=major or grid=both |
| Axis placement | axis 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, andgridbelong in\begin{axis}[...]. - Leaving out the package: The
axisenvironment and\addplotare provided bypgfplots, so load it with\usepackage{pgfplots}. - Expecting limits to choose the tick marks:
xminandxmaxset the visible range; usextickwhen you want to control tick positions explicitly. - Mixing up plot bounds and function domain:
domain=-2:2controls where the function is sampled. Thexminandxmaxkeys control the displayed axis range. They often match, but they serve different purposes. - Scaling the whole picture to set the graph dimensions: Prefer
widthandheightin 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
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.