لَآ إِلَـٰهَ إِلَّا هُوَ
LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.

# Python Data Science Matplotlib Pie Charts In Matplotlib

In Matplotlib the pie() function can be use to draw pie charts.

The pie chart draws one piece (called a wedge) for each value in the array. By default the plotting of the first wedge starts from the x-axis and move anti-clockwise.

Example 1: A simple pie chart

Code

import matplotlib.pyplot as plt
import numpy as np

y = np.array([30, 25, 35, 10])

plt.pie(y)
plt.show()

the output will be

Note: As you can see the pie chart draws one piece (called a wedge) for each value in the array (in this case [30, 25, 35, 10]).

Adding Labels In Pie Chart

Labels can be added to the pie chart with the label parameter.

The label parameter must be an array with one label for each wedge.

Example 2: Add labels to the above pie chart.

Code

import matplotlib.pyplot as plt
import numpy as np

y = np.array([30, 25, 35, 10])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]

plt.pie(y, labels = mylabels)
plt.show()

the output will be

Start Angle

As a rule the default start angle is at the x-axis, but you can change the start angle by specifying a startangle parameter.

Example 3: Start the first wedge at 90 degree in the above example.

Code

import matplotlib.pyplot as plt
import numpy as np

y = np.array([30, 25, 35, 10])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]

plt.pie(y, labels = mylabels, startangle = 90)
plt.show()

the output will be

Explode

The Explode parameter comes into picture when you want one of the wedges to stand out. The explode parameter allows you to do that.

The explode parameter, if specified, and not None, must be an array with one value for each wedge.

Each value represents how far from the center each wedge is displayed.

Example 4: Pull the "cherries" wedge 0.1 from the center of the pie.

Code

import matplotlib.pyplot as plt
import numpy as np

y = np.array([30, 25, 35, 10])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
myexplode = [0, 0, 0.1, 0]

plt.pie(y, labels = mylabels, startangle = 90, explode = myexplode)
plt.show()

the output will be

You can add a shadow to the pie chart by setting the shadows parameter to True.

Example 5: Add shadow parameter to the above example.

Code

import matplotlib.pyplot as plt
import numpy as np

y = np.array([30, 25, 35, 10])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
myexplode = [0, 0, 0.1, 0]

plt.pie(y, labels = mylabels, startangle = 90, explode = myexplode, shadow = True)
plt.show()

the output will be

You can set the color of each wedge with the colors parameter.

The colors parameter, if specified, must be an array with one value for each wedge.

Example 6: Specify a new color for each wedge in the above example.

Code

import matplotlib.pyplot as plt
import numpy as np

y = np.array([30, 25, 35, 10])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
mycolors = ["red", "hotpink", "m", "#4CAF50"]
myexplode = [0, 0, 0.1, 0]

plt.pie(y, labels = mylabels, startangle = 90, explode = myexplode, shadow = True, colors = mycolors)
plt.show()

the output will be

You can use Hexadecimal color values, any of the 140 supported color names, or one of these shortcuts.

```'r' - Red
'g' - Green
'b' - Blue
'c' - Cyan
'm' - Magenta
'y' - Yellow
'k' - Black
'w' - White
```

Adding Legend to Pie Charts

The legend() is used to add a list of explanation for each wedge.

Example 7: Add a legend to the above example.

Code

import matplotlib.pyplot as plt
import numpy as np

y = np.array([30, 25, 35, 10])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
mycolors = ["red", "hotpink", "m", "#4CAF50"]
myexplode = [0, 0, 0.1, 0]

plt.pie(y, labels = mylabels, startangle = 90, explode = myexplode, shadow = True, colors = mycolors)

plt.legend()
plt.show()

the output will be

In order to add a legend with a header you can add a header to the legend, add the title parameter to the legend function..

Example 8: Add a legend with a header.

Code

import matplotlib.pyplot as plt
import numpy as np

y = np.array([30, 25, 35, 10])
mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
mycolors = ["red", "hotpink", "m", "#4CAF50"]
myexplode = [0, 0, 0.1, 0]

plt.pie(y, labels = mylabels, startangle = 90, explode = myexplode, shadow = True, colors = mycolors)

plt.legend(title = "My Diet Fruits List:")
plt.show()

the output will be