# How to plot a pie chart in Latex

Data is very important, but more important is how you show that data. There are many ways to show data information: tables, bar charts, scatter plots and of course pie charts. In this tutorial, we will draw a pie chart in LaTeX using a dedicated package based on TikZ and named pgf-pie package.

## 1. Set up of the pgf-pie package

As mentioned before, the $\verb|pgf-pie.sty|$ is based on TikZ it is no necessary to load the $\verb|tikz|$ package since it automatically loaded by the $\verb|pgf-pie.sty|$. Here is the code that shows how to set up the preamble and the tikzpicture environment.
\documentclass[dvipsnames]{standalone}
\usepackage{pgf-pie}

\begin{document}
\begin{tikzpicture}
% here comes the code
\end{tikzpicture}
\end{document}

Notice that we can use the $\verb|tikzpicture|$ environment without loading the $\verb|tikz|$ package. It should be also noted that we have added the \verb|dvipsnames| to the document class so we can use custom colour names in the document. Check this link for a detailed list.

## 2. Plotting a pie chart in $\LaTeX$

Let’s consider the data shown in the next table, which represents the percentage of smartphones sold by companies for the 2019. We can plot this data as a pie chart using the next command:
\pie[options] {
percentaje_1/ label_1,
percentaje_2/ label_2,
percentaje_3/ label_3,
.
.
.
}

The $\verb|\pie|$ command is very simple to use. You just need to write the $\verb|percentage|$ and the label separated by a slash. Of course, you have to check that the percentages sum up to $100\%$, otherwise you will get an empty slice of pie.

The code below shows the how the command $\verb|\pie|$ works for our example. Here we haven’t used any options, so the package computes the size, the location of the labels and the colours.

\documentclass[dvipsnames]{standalone}

\usepackage{pgf-pie}

\begin{document}
\begin{tikzpicture}
\pie[] {
22.7/ Samsung,
17.6/ Huawei,
10.1/ Apple,
9.7/ Xiaomi,
8.9/ OPPO,
31.0/ Other}
\end{tikzpicture}
\end{document} 

## 3. Setting the appearance of the pie chart in $\LaTeX$

The default result may not be what we exactly want. Luckily, the package is flexible and allows us to change the position of the labels, the size of the plot and the colours. Let’s take a look to the available options of the $\verb|pgf-pie|$ package:
• $\verb|text|$: This option can take three different values: $\verb|pin|$, $\verb|inside|$ or $\verb|legend|$. The first value draws a line from the slice of pie to the label, the second one puts the labels inside the slices of pies and the last one makes a legend with the labels at the right of the pie chart.
• $\verb|color|$: This options allows us to specify a colour for each slice of pie.
• $\verb|radius|$: the default value for this option is $\verb|3|$. You can increase or decrease the radius of the pie changing this value.
• $\verb|pos = x,y|$: Sets the centre of the pie to the $\verb|(x,y)|$ coordinates.
• $\verb|rotate = theta|$: Rotates the pie the amount $\verb|tetha|$ degrees.
• $\verb|scale font:|$ Automatically scales the font to fit in the slices.
• $\verb|sum:|$ You can pass to this option any number to make it the reference instead the default value 100. Also you can pass the key $\verb|auto|$, so the compiler makes the reference in function of the given values for the slices.
• $\verb|explode:|$ Makes that the slices separate one from each other.

In the next code, we have used the $\verb|text = inside|$ option and also we have changed the scheme colour to a kind of more discreet green gradient, the colour used was $\verb|GreenYellow|$ from the $\verb|dvipsnames|$ option. The obtained illustration is shown below.

\documentclass[dvipsnames]{standalone}

\usepackage{pgf-pie}

\begin{document}
\begin{tikzpicture}
\pie[
text = inside,
color = {
GreenYellow!70,
GreenYellow!60,
GreenYellow!50,
GreenYellow!40,
GreenYellow!30,
GreenYellow!20},
] {
22.7/ Samsung,
17.6/ Huawei,
10.1/ Apple,
9.7/ Xiaomi,
8.9/ OPPO,
31.0/ Other
}
\end{tikzpicture}
\end{document}


## 4. Different chart styles

With the $\verb|pgf-pie|$ package we can plot not only pie charts, but also rectangular and cloud charts. Let’s take a look to a rectangular and cloud charts obtained by using the $\verb|square|$ and $cloud$ options.

\documentclass[dvipsnames]{standalone}

\usepackage{pgf-pie}

\begin{document}
\begin{tikzpicture}[scale = 1.25]
\pie[
square,
text = legend,
] {
22.7/ Samsung,
17.6/ Huawei,
10.1/ Apple,
9.7/ Xiaomi,
8.9/ OPPO,
31.0/ Other
}
\end{tikzpicture}
\end{document}

\documentclass[dvipsnames]{standalone}

\usepackage{pgf-pie}

\begin{document}
\begin{tikzpicture}
\pie[
cloud,
text = legend,
] {
22.7/ Samsung,
17.6/ Huawei,
10.1/ Apple,
9.7/ Xiaomi,
8.9/ OPPO,
31.0/ Other
}
\end{tikzpicture}
\end{document}