What PGFPlots does
PGFPlots builds on TikZ and reads data points, so you do not place bars or points by hand. You describe an axis, add one \addplot per data series, and PGFPlots works out the scale, ticks, and bar positions.
Overleaf includes PGFPlots, so you can follow the examples there without installing anything.
Set up a PGFPlots axis
Load the package and set a compatibility level:
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
The compat setting tells PGFPlots which defaults to use. Set it to 1.18 or newer so the defaults match a recent release.
Each chart goes inside a tikzpicture, and the axis environment sits inside that.
Draw a basic bar chart
\begin{tikzpicture}
\begin{axis}[
ybar,
bar width=14pt,
symbolic x coords={Jan, Feb, Mar, Apr},
xtick=data,
ymin=0,
ylabel={Units sold},
nodes near coords,
]
\addplot coordinates {(Jan,30) (Feb,45) (Mar,25) (Apr,50)};
\end{axis}
\end{tikzpicture}
ybar draws vertical bars. symbolic x coords lets you use names instead of numbers on the x axis, and xtick=data puts a tick under each name. nodes near coords prints each value above its bar.
Every \addplot line ends with a semicolon. Leaving it out is a common cause of errors that appear on a later line.
Compare several series with grouped bars
Add a second \addplot and a legend entry for each series. In ybar mode, PGFPlots places the bars for each series side by side:
\begin{tikzpicture}
\begin{axis}[
ybar,
symbolic x coords={North, South, East, West},
xtick=data,
ymin=0,
enlarge x limits=0.2,
legend pos=north west,
]
\addplot coordinates {(North,12) (South,18) (East,9) (West,15)};
\addlegendentry{2024}
\addplot coordinates {(North,14) (South,16) (East,11) (West,19)};
\addlegendentry{2025}
\end{axis}
\end{tikzpicture}
enlarge x limits=0.2 adds space at each end of the x axis. legend pos=north west moves the legend to the upper left.
Draw horizontal bars
Use xbar to turn the chart on its side. The category names now go on the y axis, and each coordinate is written as (value, category):
\begin{tikzpicture}
\begin{axis}[
xbar,
symbolic y coords={Alpha, Beta, Gamma},
ytick=data,
xmin=0,
xlabel={Responses},
nodes near coords,
]
\addplot coordinates {(42,Alpha) (35,Beta) (23,Gamma)};
\end{axis}
\end{tikzpicture}
Draw a line graph from data points
Use \addplot with coordinates for measured points. Add mark for a symbol at each point, and a style such as dashed for a second series:
\begin{tikzpicture}
\begin{axis}[
xlabel={Time (s)},
ylabel={Distance (m)},
grid=major,
legend pos=north west,
]
\addplot[blue, mark=*] coordinates {(0,0) (1,2) (2,5) (3,9) (4,14)};
\addlegendentry{Run A}
\addplot[red, dashed, mark=square*] coordinates {(0,0) (1,1.5) (2,4) (3,7) (4,11)};
\addlegendentry{Run B}
\end{axis}
\end{tikzpicture}
Plot a function as a line
For a formula instead of data points, put the expression in braces and set the domain and the number of samples:
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},
ylabel={$f(x)$},
domain=-2:2,
samples=100,
grid=major,
]
\addplot[thick, purple] {x^2};
\end{axis}
\end{tikzpicture}
Labels, legends, and axis options
These options cover most adjustments:
| Option | What it does |
|---|---|
xlabel, ylabel |
Axis titles |
title |
Chart title |
legend pos=south east |
Moves the legend (any compass position works) |
grid=major |
Adds gridlines |
xmin, xmax, ymin, ymax |
Fixes the axis range |
width, height |
Sets the size of the axis |
Common errors
- An error appears on a later line. A missing semicolon at the end of an
\addplotline often causes this. - Symbolic names are not recognized. Names in
coordinatesmust match the entries insymbolic x coordsexactly, including capitals. - The chart looks different from the examples. Check the
compatsetting in the preamble. - Bars do not appear as expected. Check that the bar type matches the axis:
ybartakes symbolic x names, andxbartakes symbolic y names.
Where to go next
- For parts of a whole rather than comparisons, see how to draw a pie chart in LaTeX.
- For other diagram types, the block diagrams and flowcharts guide covers TikZ basics.
Frequently asked questions
Can I use PGFPlots in Overleaf?
Yes. Add \usepackage{pgfplots} and \pgfplotsset{compat=1.18} to the preamble, then paste any example above into the document body.
Should I use a bar chart or a pie chart?
Use bars when you compare several values or show change across categories. Pie charts are better for showing how a few parts make up a whole.
How do I load data from a file?
PGFPlots can read a CSV file with \addplot table. Add col sep=comma and name the columns with x and y options, such as \addplot table[col sep=comma, x=month, y=sales] {data.csv};.