# Plot a Pie Chart with Matplotlib

Pie charts are great for visualizing the distribution of counts among different values of a categorical field. For example, the distribution of T-shirts sold based on size (small, medium, large, etc). In this tutorial, we’ll look at how to plot a pie chart in python using matplotlib.

Matplotlib is a powerful visualization library in python and comes up with a number of different charting options. You can plot a pie chart in matplotlib using the pyplot’s `pie()` function. The following is the syntax:

``````import matplotlib.pyplot as plt
plt.pie(x, labels)
plt.show()``````

Here, `x` is an array or sequence of counts (the wedge sizes in the pie) and `labels` is the sequence of string labels to be used for each portion (wedge). `labels` is an optional parameter which is `None` by default.

Let’s look at some examples of using matplotlib’s `pie()` function to plot a pie chart.

Let’s say you have the sales figures for T-shirts of different sizes for a brand and want to plot a pie chart of the distribution across sizes.

``````import matplotlib.pyplot as plt

# T-shirt sales for sizes Small, Medium, and Large
sales = [450, 800, 760]
lables = ["Small", "Medium", "Large"]

# plot a pie chart
plt.pie(sales, labels=lables)
plt.show()``````

Output:

Here, we only pass the values and the labels for the pie chart and use all other parameters as default. We can see that Medium and Large had more sales than Small but it’s difficult to differentiate Medium and Large from the above figure.

You can make the above pie chart more informative by adding data labels to each wedge (or portion) using the `autopct` parameter. For example, if you want to have the % distribution of each portion you can pass a format string to `autopct`

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

``````import matplotlib.pyplot as plt

# T-shirt sales for sizes Small, Medium, and Large
sales = [450, 800, 760]
lables = ["Small", "Medium", "Large"]

# plot a pie chart
plt.pie(sales, labels=lables, autopct="%1.1f%%")
plt.title("T-Shirt sales distribution")
plt.show()``````

Output:

The labels here provide information on the percentage distribution of sales across the three T-shirt sizes. We can now say that sales for size Medium outperformed that of Large by a small difference. Also notice that we added a title to the chart to make its context more clear.

You can specify custom colors for your pie chart with the `colors` parameter. Pass the colors you want for your pie chart as a sequence to `colors`. See the example below:

``````import matplotlib.pyplot as plt

# T-shirt sales for sizes Small, Medium, and Large
sales = [450, 800, 760]
lables = ["Small", "Medium", "Large"]
colors = ["wheat", "lavender", "lightblue"]

# plot a pie chart
plt.pie(sales, labels=lables, autopct="%1.1f%%", colors=colors)
plt.title("T-Shirt sales distribution")
plt.show()``````

Output:

You can see that portions small, medium, and large are colored wheat, lavender, and lightblue respectively. For a complete list of matplotlib’s named colors, refer to this guide.

There are a number of other formatting options available as well for matplotlib’s pie chart. You can “explode” (separate out) one or more portions of the pie chart with the `explode` parameter. You can provide wedge properties like line width, edge color, etc. as a dictionary to the `wedgeprops` parameter. There’s also an option to set text properties using the `textprops` parameter.

Let’s say we want to apply the following formatting options to the above pie chart:

• Explode the wedge for the Small category.
• Add gray line borders for the chart.
• Make the text bold.
``````import matplotlib.pyplot as plt

# T-shirt sales for sizes Small, Medium, and Large
sales = [450, 800, 760]
# wedge labels
lables = ["Small", "Medium", "Large"]
# wedge colors
colors = ["wheat", "lavender", "lightblue"]
# explode the wedge for Small
explode = [0.2, 0, 0]
# line properties
wedge_props = {"linewidth": 2, "edgecolor": "gray"}
# text properties
text_props = {"weight": "bold"}

# plot a pie chart
plt.pie(sales,
labels=lables,
autopct="%1.1f%%",
colors=colors,
explode=explode,
wedgeprops=wedge_props,
textprops=text_props)
plt.title("T-Shirt sales distribution")
plt.show()``````

Output:

The above is the resulting pie chart. There are a few other formatting options as well, for more refer to the official documentation.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having matplotlib version 3.2.2