The matplotlib library is a very powerful data visualization tool. It provides us with the possibility to design various charts and plots, including pie charts.
A pie chart is a circular plot that shows how data items relate to one another. The pie chart total area is the data percentage. The area of slices (also called wedges) represents the percentage of data parts and shows the relation between them. Pie charts are good when you need to compare parts of a whole. For example, pie charts work well with categorical data so that each category can be represented as a different slice.
Creating a pie chart
To create a pie chart using matplotlib, refer to the pie()function. You can do it like this:
plt.pie(data)where data is an array of data values that you want to plot.
There are many other optional parameters that you can pass to plt.pie(). Some of them include:
explodeallows separating slices of a pie chart;labelsis a list of strings that sets the label of each corresponding slice;labeldistancedetermines the radial distance at which pie labels are drawn (default is1.1);colorcolors slices;autopctlabels a slice with its numerical value;shadowcreates the shadow of a slice;startanglelets you choose the starting angle of a plot;wedgepropslets you tune various slice parameters;radiussets the radius of the circle; in other words, it determines its width (by default, it is1).
We will discuss the above-mentioned parameters in the following sections. However, there are plenty of other parameters that you can work with. You can check them out in the Official Matplotlib Documentation.
Let's create our very first pie chart. Assume our data represents the result of a survey on a favorite ice cream flavor:
import matplotlib.pyplot as plt
data = [55, 27, 15, 3]
plt.pie(data)
plt.show()As you can see, data values alone are enough to create a simple pie chart. It turned out not that descriptive, but we see that one of the categories dominates. To make the chart more illustrative, let's tweak some parameters.
Labels
Adding labels to a pie chart is pretty straightforward; you need to pass a list of strings with labels corresponding to the list of values:
labels = ['Chocolate', 'Vanilla', 'Strawberry', 'Other']
plt.pie(data, labels=labels)As with any other plot in the matplotlib library, you can add a legend, title the whole graph, and add labels to the x- and y-axes. Y-axes are rarely used with pie charts:
import matplotlib.pyplot as plt
data = [55, 27, 15, 3]
labels = ['Chocolate', 'Vanilla', 'Strawberry', 'Other']
plt.figure(figsize=(9, 7))
plt.pie(data)
plt.title('The results of the icecream survey', fontsize=14)
plt.legend(labels)
plt.show()Explode
The explode parameter in pie charts separates a slice from the main pie. To highlight some of the fractions, you need to pass a list of values to the explode parameter. The values determine how far you want to separate this category from the main pie. Let's see how we can apply this to the Vanilla category:
explode = [0.0, 0.08, 0.0, 0.0]
plt.pie(data, explode=explode, labels=labels)Note that in our list of explode values, we set the second item (it corresponds to the Vanilla category) to 0.08 or 8%. You can change this parameter for any number of categories at the same time.
Colors and shadows
To make our graph even more illustrative, we can pass a list of colors to the color parameter and add a shadow underneath by setting the shadow to True:
colors = ['saddlebrown', 'wheat', 'crimson', 'lightgrey']
plt.pie(data,
explode=explode,
labels=labels,
colors=colors,
shadow=True)You can find a list of the colors supported by matplotlib in the Official Documentation.
Display the value
When creating a pie chart, the autopct parameter displays the percent value on the slices. To do it, you need to use Python string formatting. For example, if autopct is %.2f, then for each pie slice, the format string is %.2f, where % is a special character that tells where to type the value, f sets the result to be a floating-point type, and the .2 sets a limit to only 2 digits after the point. Let's have a look:
plt.pie(
data,
explode=explode,
labels=labels,
colors=colors,
autopct='%.1f%%', # here we also add the % sign
shadow=True,
)As you can see, even though our dataset has only integers, autopct converts them to floats and adds an extra zero after a point.
Starting angle
By default, pie charts are plotted from the x-axis; slices are plotted counterclockwise:
The startangle parameter lets us define the angle where we want to initiate our pie (the default angle is 0). You can choose whether you want to plot the slices counterclockwise by changing the counterclock parameter; it is True by default.
Let's assume we want the slices to start at 90 degrees. Here's how to do it:
import matplotlib.pyplot as plt
data = [55, 27, 15, 3]
labels = ['Chocolate', 'Vanilla', 'Strawberry', 'Other']
fig, axes = plt.subplots(1, 2, figsize=(10, 6))
ax1, ax2 = axes
ax1.pie(data, labels=labels, colors=colors, startangle=90)
ax2.pie(data, labels=labels, colors=colors, startangle=90, counterclock=False)
ax1.set_title('Starting the plot at 90°')
ax2.set_title('Plotting clockwise')
plt.show()The doughnut chart
A doughnut chart is very similar to a pie chart. However, since a doughnut chart has an opening at the center, it makes slices look more like bars. Our focus switches from proportions and areas to the slice length.
Unfortunately, there is no specific method for plotting a doughnut chart in the matplotlib library. But we can use the wedgeprops parameter to define the width of the wedges. Note that it takes a dictionary as an argument:
plt.pie(data,
labels=labels,
colors=colors,
startangle=90,
wedgeprops={'width': 0.2})The wedgeprops parameter also lets you change various pie slice features, such as linewidth, edgecolor, alpha (for transparency), and many others. You can find the complete list in the Official Documentation.
Even though a pie chart and a doughnut chart both serve the same purpose, a doughnut chart can output more than one set of data. How is that possible? Well, thanks to the open center, we can easily plot multiple doughnut charts to compare two or more sets. Let's assume that for some reason, we want to compare the results of two surveys: a favorite ice cream flavor and a favorite pet. Let's see how we can do it:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(8, 6))
width = 0.3
icecream_data = [55, 27, 15, 3]
icecream_labels = ['Chocolate', 'Vanilla', 'Strawberry', 'Other']
icecream_colors = ['saddlebrown', 'wheat', 'crimson', 'lightgrey']
icecream_pie = ax.pie(
icecream_data,
radius=1,
labels=icecream_labels,
colors=icecream_colors,
wedgeprops={'width': width},
)
pets_data = [45, 41, 10, 4]
pets_labels = ['Dogs', 'Cats', 'Parrots', 'Other']
pets_colors = ['orange', 'teal', 'powderblue', 'grey']
pets_pie = ax.pie(
pets_data,
radius=1 - width,
labels=pets_labels,
labeldistance=0.7,
colors=pets_colors,
wedgeprops={'width': width},
)
plt.show()In the plot above, we keep the default radius value for our outer chart and make the inner chart smaller by setting the radius to 1-width. We also change the labeldistance of the inner doughnut labels so that they are a bit closer to the center.
Conclusion
In this topic, we've gone over the design of a simple pie and doughnut chart in matplotlib. We've also learned how to customize some of its features for both esthetic and practical purposes.