Home » Matplotlib Pie Chart in Python

Matplotlib Pie Chart in Python

Oracle Java Certification
Java SE 11 Programmer II [1Z0-816] Practice Tests
Java SE 11 Programmer I [1Z0-815] Practice Tests
Java SE 11 Developer (Upgrade) [1Z0-817]
Spring Framework Basics Video Course
1 Year Subscription

In this article we looking at creating pie charts in python using matplotlib

Pie Chart

matplotlib.pyplot.pie(x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=None, radius=None, counterclock=True, wedgeprops=None, textprops=None, center=(0, 0), frame=False, rotatelabels=False, *, data=None)

Make a pie chart of array x. The fractional area of each wedge is given by x/sum(x). If sum(x) < 1, then the values of x give the fractional area directly and the array will not be normalized. The resulting pie will have an empty wedge of size 1 - sum(x).

The wedges are plotted counterclockwise, by default starting from the x-axis.

Parameters:
x : array-like

The wedge sizes.

explode : array-like, optional, default: None

If not None, is a len(x) array which specifies the fraction of the radius with which to offset each wedge.

labels : list, optional, default: None

A sequence of strings providing the labels for each wedge

colors : array-like, optional, default: None

A sequence of matplotlib color args through which the pie chart will cycle. If None, will use the colors in the currently active cycle.

autopct : None (default), string, or function, optional

If not None, is a string or function used to label the wedges with their numeric value. The label will be placed inside the wedge. If it is a format string, the label will be fmt%pct. If it is a function, it will be called.

pctdistance : float, optional, default: 0.6

The ratio between the center of each pie slice and the start of the text generated by autopct. Ignored if autopct is None.

shadow : bool, optional, default: False

Draw a shadow beneath the pie.

labeldistance : float or None, optional, default: 1.1

The radial distance at which the pie labels are drawn. If set to None, label are not drawn, but are stored for use in legend()

startangle : float, optional, default: None

If not None, rotates the start of the pie chart by angle degrees counterclockwise from the x-axis.

radius : float, optional, default: None

The radius of the pie, if radius is None it will be set to 1.

counterclock : bool, optional, default: True

Specify fractions direction, clockwise or counterclockwise.

wedgeprops : dict, optional, default: None

Dict of arguments passed to the wedge objects making the pie. For example, you can pass in wedgeprops = {'linewidth': 3} to set the width of the wedge border lines equal to 3. For more details, look at the doc/arguments of the wedge object. By default clip_on=False.

textprops : dict, optional, default: None

Dict of arguments to pass to the text objects.

center : list of float, optional, default: (0, 0)

Center position of the chart. Takes value (0, 0) or is a sequence of 2 scalars.

frame : bool, optional, default: False

Plot axes frame with the chart if true.

rotatelabels : bool, optional, default: False

Rotate each label to the angle of the corresponding slice if true.

We will cover a few of the parameters in the following examples

Examples

Basic example

Here is a basic example which will create a basic pie chart

import matplotlib.pyplot as plot

x = [30, 9, 52, 12, 24]
y = ['Apples', 'Pears', 'Oranges', 'Lemons', 'Limes']

plot.pie(x, labels = y)

plot.show()

This will display the following

pie chart example

pie chart example

matplotlib Pie Chart title

The Python pyplot has a title method, which helps to assign a title or heading for the pie chart.

Let see an example

import matplotlib.pyplot as plot

x = [30, 9, 52, 12, 24]
y = ['Apples', 'Pears', 'Oranges', 'Lemons', 'Limes']

plot.pie(x, labels = y)
plot.title("Fruit popularity",
     color = 'blue', fontweight = 'bold', fontsize = '16')
plot.show()

Which will give you a pie chart like this

pie chart example with title

pie chart example with title

matplotlib Pie chart colors

We can change the colors of the wedges as well by using the Python colors argument to assign our own to each wedge.

import matplotlib.pyplot as plot

x = [30, 9, 52, 12, 24]
y = ['Apples', 'Pears', 'Oranges', 'Lemons', 'Limes']
color_list = ['red', 'green', 'blue', 'yellow', 'cyan']

plot.pie(x, labels = y, colors = color_list)
plot.title("Fruit popularity",
     color = 'blue', fontweight = 'bold', fontsize = '16')
plot.show()

This is what i saw

pie chart example colors

pie chart example colors

matplotlib Pie chart percentages

The autopct argument shows the percentage of each wedge. You can use the string format to vary the display of percentage values. For example, %1.2f%% will return value 15.24%, and .1f% returns 15.2%.

import matplotlib.pyplot as plot

x = [30, 9, 52, 12, 24]
y = ['Apples', 'Pears', 'Oranges', 'Lemons', 'Limes']
color_list = ['red', 'green', 'blue', 'yellow', 'cyan']

plot.pie(x, labels = y, colors = color_list, autopct = '%1.1f%%')
plot.title("Fruit popularity",
     color = 'blue', fontweight = 'bold', fontsize = '16')
plot.show()

This displayed the following pie chart

pie chart example percentages

pie chart example percentages

matplotlib Pie chart Slice out

The explode argument in pyplot pie decides which part should explode. This argument accepts a tuple of numeric values, and each non-zero value represents the distance of that slice from the center.

import matplotlib.pyplot as plot

x = [30, 9, 52, 12, 24]
y = ['Apples', 'Pears', 'Oranges', 'Lemons', 'Limes']
color_list = ['red', 'green', 'blue', 'yellow', 'cyan']
explode_value = (0.2, 0.1, 0.1, 0.2, 0.1)

plot.pie(x, labels = y, colors = color_list, autopct = '%1.1f%%', explode = explode_value)
plot.title("Fruit popularity",
     color = 'blue', fontweight = 'bold', fontsize = '16')
plot.show()

This gave the following pie chart

matplotlib Rotating Python pie chart

The startangle argument in the pie function allows choosing the chart starting position. It means you can rotate the pie chart based on the angle of degrees you provided.

In this example, we are using 180 degrees so that the chart start position will start at 180 degrees. Next, we used the shadow argument and assigned True value to it, so a shadow will be displayed on the pie chart.

There is another argument called counterclock. It means the pie chart will draw in counterclockwise but we will set this to false rather than the default of true, lets see all of this.

import matplotlib.pyplot as plot

x = [30, 9, 52, 12, 24]
y = ['Apples', 'Pears', 'Oranges', 'Lemons', 'Limes']
color_list = ['red', 'green', 'blue', 'yellow', 'cyan']
explode_value = (0.2, 0.1, 0.1, 0.2, 0.1)

plot.pie(x, labels = y, colors = color_list, autopct = '%1.1f%%',
    explode = explode_value,
    shadow = True,
    startangle = 90,
    counterclock = False)

plot.title("Fruit popularity",
     color = 'blue', fontweight = 'bold', fontsize = '16')

plot.show()

This gave me the following pie chart

Links

github for these examples

matplotlib documentation

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More