Despite being misunderstood, Pie charts appears frequently in most visualization reports. Pie charts are used to visualize the part of a whole comparison. They show the contribution of each category to the overall value. When properly used pie charts play an important part in presenting insights to users. In this post we will look at when to use pie charts, the best practices and how to create Pie Chart in Matplotlib. ## When to Use Pie Charts

Pie chart is used to visualize the part of a whole relationship for categorical variable.

## How to Use Pie Charts

1. Use fewer slices as much as possible.
2. Use annotations to provide actual values and percentage of each pie.
3. Select colours that show distinct comparison between pies.
4. Use legends.
5. Avoid data with negative values.

## Pie Charts in Matplotlib

Create DataFrame

```                    ```
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.style.use(‘ggplot’) # Other sytles to use; fivethirtyeight
score_df = pd.DataFrame(
{
"Students": ["Tom", "Peter","Simon", "Mary", "Jane","King","Hillary","Ethan","Page"],
"Math": [79.00, 67.00,80.00, 84.00, 70.00,60.00,90.00,76.00,75],
"Physics":[63.00, 98, 60.00, 90,84.00, 77.00,55.00,70,66.00],
"Computer":[84.00,78.00, 57.00, 88.00, 75.00,93.00,92.00,98.00,90.00],
},
index=["Tom", "Peter","Simon", "Mary", "Jane","King","Hillary","Ethan","Page"]
)

score_df['Total']=score_df[['Math','Physics','Computer']].apply(np.sum,axis=1)
score_df
```
``` Simple Pie Chart

```                    ```
plt.figure(figsize=(20,15)) # Set figure size
plt.pie(score_df['Total'],labels=score_df['Students'])
plt.show()
```
``` Format Pie Chart

First we add percentages with autopct to show the contribution of each students to the overal score. shadow=True adds shadow to the pie. Explode 1st and 4th pies.

```                    ```
plt.figure(figsize=(20,15)) # Set figure size
explode_student=[0.1,0,0,0.3,0,0,0,0,0]
plt.legend(loc='lower right',title='Students',bbox_to_anchor =(1, 0, 0.2, 1))
plt.show()
```
``` Show percentage and Actual data

First we have to create a custom function with percentages and absolute values. Then apply the function to autopct with a lambda function.

```                    ```
def percentage_and_actual_value(percentage, actual_value):
actual = int(percentage / 100.*np.sum(actual_value))
return "{:.2f}%\n{:d}".format(percentage, actual)

plt.figure(figsize=(20,15)) # Set figure size
explode_student=[0.1,0,0,0.3,0,0,0,0,0]
plt.pie(score_df['Total'],labels=score_df['Students'],autopct=lambda x: percentage_and_actual_value(x, score_df['Total']),
plt.legend(loc='lower right',title='Students',bbox_to_anchor =(1, 0, 0.2, 1))
plt.show()
```
``` For complete code check the jupyter notebook here.

## Conclusion

In this post we have looked at Pie Charts, one of the most unwanted and often branded as confusing types of visualization, pie charts plays an important role in presenting important insights about data when properly used. They are used to show the part of a whole relationship in data. We have looked at what they are, when to use them, how to use pie charts and how to create them in Matplotlib. In the next post we will look at Area Charts. To learn about the Famous Bar graph check our previous post here.

Pie Chart in Matplotlib 