\usepackage{pgfplots}
Create publication-quality plots directly in your LaTeX documents using pgfplots. From simple function plots to complex multi-panel figures with scatter data.
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:
\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}All options are passed inside the square brackets of the \begin{axis}[…] command. They can be combined freely.
| Option | Type | Description |
|---|---|---|
| title | string | Title displayed above the axis frame. |
| xlabel / ylabel | string | Labels for the x and y axes; support math mode. |
| xmin / xmax | number | Explicit lower and upper bounds of the x axis. |
| grid | major | minor | both | Draws grid lines at major ticks, minor ticks, or both. |
| xtick | list | Explicit tick positions as a comma-separated list. |
| xticklabels | list | Custom labels for each tick; supports math mode entries. |
| width / height | length | Physical size of the axis box, e.g. 10cm or 0.8\textwidth. |
| legend pos | keyword | Position of the legend: north east, south west, outer north east, etc. |
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}pgfplots supports several plot handlers through the \addplot command. The handler is selected via plot options or coordinate syntax.
Plot any mathematical expression over a range using domain and samples.
\addplot[color, thick, domain=a:b] {expr};Plot discrete data points supplied directly in the source file.
\addplot[only marks] coordinates { (x1,y1) (x2,y2) };Vertical bar chart using the ybar plot handler with coordinate pairs.
\addplot[ybar] coordinates { (1,3) (2,5) (3,2) };Load tabular data from an external CSV or whitespace-delimited file.
\addplot table {data.csv};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/coolFont 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}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}Open a ready-to-compile sine wave example directly in the FormaTeX playground. Edit it live and download the PDF — no installation required.
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.