PGFPlots and pie charts
PGFPlots draws axes with bars, lines, and points, but it does not have a pie chart type. This guide uses the pgf-pie package instead. It builds on the same PGF and TikZ engine that PGFPlots uses, so the code looks and behaves like other TikZ drawings. If you want a bar chart with PGFPlots, the bar charts and line graphs guide covers it.
Load pgf-pie and draw a basic pie chart
\documentclass{article}
\usepackage{pgf-pie}
\begin{document}
\begin{tikzpicture}
\pie{45/Math, 30/Physics, 25/Chemistry}
\end{tikzpicture}
\end{document}
Each entry is a value/label pair, separated by commas. Each slice’s angle is proportional to its value relative to the total, so you can enter raw counts or percentages. The \pie command must sit inside a tikzpicture.
Show a legend instead of slice labels
Labels printed on the slices can crowd each other when the slices are small. The text=legend option moves the labels into a legend:
\begin{tikzpicture}
\pie[text=legend]{45/Math, 30/Physics, 25/Chemistry}
\end{tikzpicture}
Resize the pie
The radius option sets the size of the pie. It combines with the legend option:
\begin{tikzpicture}
\pie[radius=2, text=legend]{45/Math, 30/Physics, 25/Chemistry}
\end{tikzpicture}
Pick a radius that fits your page. If the pie and its legend are too wide, reduce the radius.
When a pie chart works
A pie chart shows how parts add up to one whole. It works best with a few slices. When there are many small slices, or when readers need to compare values closely, a bar chart is clearer, because people judge lengths more accurately than angles. See the bar chart guide for that case.
A pie chart also needs the slices to belong to the same whole. If the values come from different totals, a bar chart avoids implying a false comparison.
Common problems
- LaTeX cannot find
pgf-pie.sty. Your TeX installation does not include the package. Install it with your distribution’s package manager, as described in how to install LaTeX packages. - Labels overlap. Switch to
text=legend, as shown above. - An error mentions
\pie. Wrap\piein atikzpictureenvironment. It does not work on its own. - The pie runs off the page. Reduce the
radius.
Frequently asked questions
Does PGFPlots have a pie chart?
No. PGFPlots covers axis-based plots such as bars, lines, and scatter plots. For a pie chart in LaTeX, use pgf-pie, as shown above.
Should I use a pie chart or a bar chart?
Use a pie chart for a few parts of one whole. Use a bar chart when you compare several values, or when the categories do not add up to a whole.