FormaTeX

\usepackage{pgfplots}

pgfplots Graphs and Plots in LaTeX

Create publication-quality plots directly in your LaTeX documents using pgfplots. From simple function plots to complex multi-panel figures with scatter data.

TikZ Guide

Getting Started

Add \usepackage{pgfplots} to your preamble and set the compatibility level with \pgfplotsset{compat=1.18}. Then wrap your plot in a tikzpicture and an axis environment. Key options explained:

  • titleText displayed above the plot frame
  • xlabel / ylabelAxis labels; math mode is supported
  • gridmajor, minor, or both — draws grid lines at ticks
  • domain=a:bThe x range over which the function is evaluated
  • samplesNumber of evaluation points — higher = smoother curve
  • \addplot[opts] {expr};Draws a plot; options include color, thick, dashed
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    title={$f(x) = x^{2}$},
    xlabel={$x$},
    ylabel={$f(x)$},
    grid=major,
    width=8cm,
    height=6cm,
  ]
    \addplot[blue, thick, domain=-3:3, samples=100] {x^2};
  \end{axis}
\end{tikzpicture}

\end{document}

Axis Environment Options

All options are passed inside the square brackets of the \begin{axis}[…] command. They can be combined freely.

OptionTypeDescription
titlestringTitle displayed above the axis frame.
xlabel / ylabelstringLabels for the x and y axes; support math mode.
xmin / xmaxnumberExplicit lower and upper bounds of the x axis.
gridmajor | minor | bothDraws grid lines at major ticks, minor ticks, or both.
xticklistExplicit tick positions as a comma-separated list.
xticklabelslistCustom labels for each tick; supports math mode entries.
width / heightlengthPhysical size of the axis box, e.g. 10cm or 0.8\textwidth.
legend poskeywordPosition of the legend: north east, south west, outer north east, etc.

Multiple Plots & Legends

Add multiple \addplot commands inside the same axis environment to overlay curves. Follow each with \addlegendentry{label} to populate the legend automatically. The legend pos option controls placement — use outer north east to place the legend outside the axis frame and avoid overlapping your data.

Custom tick labels with xticklabels accept math-mode fractions such as $\frac{\pi}{2}$. The minor tick num=1 option inserts one minor tick between each pair of major ticks.

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

\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    title={Trigonometric Functions},
    xlabel={$x$ (radians)},
    ylabel={$y$},
    xmin=0, xmax=6.2832,
    ymin=-1.3, ymax=1.3,
    xtick={0,1.5708,3.1416,4.7124,6.2832},
    xticklabels={$0$, $\frac{\pi}{2}$, $\pi$, $\frac{3\pi}{2}$, $2\pi$},
    grid=both,
    minor tick num=1,
    legend pos=outer north east,
    width=10cm,
    height=6cm,
  ]
    \addplot[blue, thick, domain=0:6.2832, samples=200] {sin(deg(x))};
    \addlegendentry{$\sin(x)$}
    \addplot[red,  thick, domain=0:6.2832, samples=200] {cos(deg(x))};
    \addlegendentry{$\cos(x)$}
  \end{axis}
\end{tikzpicture}

\end{document}

Plot Types

pgfplots supports several plot handlers through the \addplot command. The handler is selected via plot options or coordinate syntax.

Function plot

Plot any mathematical expression over a range using domain and samples.

\addplot[color, thick, domain=a:b] {expr};

Scatter / coordinates

Plot discrete data points supplied directly in the source file.

\addplot[only marks] coordinates { (x1,y1) (x2,y2) };

Bar chart

Vertical bar chart using the ybar plot handler with coordinate pairs.

\addplot[ybar] coordinates { (1,3) (2,5) (3,2) };

From file

Load tabular data from an external CSV or whitespace-delimited file.

\addplot table {data.csv};

Customizing Axis Styles

Three common style adjustments that make plots look polished in academic documents.

Color scheme

Apply a pgfplots colormap to a surface or scatter plot by adding colormap/cool as an axis option.

colormap/cool

Font size

Scale tick labels independently from the document font using tick label style={font=\small} inside the axis options.

tick label style={font=\small}

Grid style

Customise the appearance of grid lines by setting grid style={dashed, gray!40} in the axis options.

grid style={dashed, gray!40}

Multiple Subfigures

To place two or more plots side by side in a single figure, combine pgfplots with the subcaption package. Each plot lives inside a subfigure environment. Set width=\linewidth inside the axis options so each plot fills its column. Use \hfill between subfigures to distribute horizontal space evenly.

The outer \caption on the figure environment provides the overall caption, while each subfigure gets its own sub-caption labeled (a), (b), etc. automatically.

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

\begin{document}

\begin{figure}[htbp]
  \centering
  \begin{subfigure}{0.48\textwidth}
    \begin{tikzpicture}
      \begin{axis}[width=\linewidth, title={$\sin(x)$}, grid=major]
        \addplot[blue, thick, domain=0:6.2832, samples=100] {sin(deg(x))};
      \end{axis}
    \end{tikzpicture}
    \caption{Sine function}
  \end{subfigure}
  \hfill
  \begin{subfigure}{0.48\textwidth}
    \begin{tikzpicture}
      \begin{axis}[width=\linewidth, title={$\cos(x)$}, grid=major]
        \addplot[red, thick, domain=0:6.2832, samples=100] {cos(deg(x))};
      \end{axis}
    \end{tikzpicture}
    \caption{Cosine function}
  \end{subfigure}
  \caption{Trigonometric functions side by side}
\end{figure}

\end{document}

Try the Sine Wave Plot

Open a ready-to-compile sine wave example directly in the FormaTeX playground. Edit it live and download the PDF — no installation required.

Frequently Asked Questions

Compile your pgfplots diagram

FormaTeX compiles your LaTeX in the cloud — no local TeX installation needed. Sign up free and start plotting in seconds.

One quick thing

We track anonymous usage — page views, feature usage, compilation events — to understand what works and what doesn't. No ads, no personal data, no third-party sharing.

Cookie policy