# Plotting Pie Charts in Python (Tutorial)

This is a detailed tutorial on plotting pie charts in Python. Learn to create different types of Pie Charts using the `matplotlib` module with examples.

If you’re an absolute beginner, then I highly recommend you to begin with plotting line graphs and bar charts.

## Plotting Pie Charts in Python

A Pie Chart is a great way to visualize statistics. We’ll be using the `matplotlib` module to plot the pie charts, so if you have not installed it yet on your system, simply use the following command to do so.

`pip install matplotlib`

It’s very easy to plot a simple pie chart in python using the `matplotlib` module. The `pyplot.pie()` method takes some simple arguments and creates a pie chart for you in no time!

Let’s first create a simple pie chart and then move on to create complex pie charts by doing a lot of available customizations.

### A Simple Pie Chart

I have taken the data set of market shares of the different desktop Operating Systems as of June 2020. The data is as follows.

 Operating System Worldwide Market Share Windows 77.68% OS X 17.76% Unknown 2.06% Linux 1.69% Chrome OS 0.8%

Given below is the code to plot a pie-chart for this data.

```import matplotlib.pyplot as p

#Labels
OS = ["Windows", "OS X", "Unknown", "Linux", "Chrome OS"]

#Part Values
parts = [77.68, 17.76, 2.06, 1.69, 0.8]

#Plotting the Pie Chart
p.pie(parts, labels = OS)

#Show Legend
p.legend()

#Display the Pie Chart
p.show()```

Output. This is quite a simple pie chart with just labels and nothing else. But a really useful pie-chart will have percentages or part values and some other graphical features to make it better in look and easier to understand.

Here we’ve just passed two arguments. The first one specifies the values for the parts of the pie-chart while the second one gives labels to these parts. We’ll talk about all of the different arguments that you can pass to customize the pie chart in the following section.

### Complex Pie Charts

In the above example, we’ve just used two parameters of the `pyplot.pie()` method but we can also use various other parameters to make a really great and highly customized pie chart.

Have a look at the following code that plots a beautiful pie chart for the exact same data.

```import matplotlib.pyplot as p

#Labels
OS = ["Windows", "OS X", "Unknown", "Linux", "Chrome OS"]

#Part Values
parts = [77.68, 17.76, 2.06, 1.69, 0.8]

#Each Part Color
colors = ["Pink", "Grey", "Blue", "Orange", "Yellow"]

#Wedge Properties
wedgeprops = {
'linewidth': 0.6
}

#Text properties
textprops = {
'fontsize': 10
}

#Explode Parts
explode = [0, 0.05, 0.1, 0.15, 0.2]

#Plotting the Pie Chart
p.pie(parts, labels = OS, radius = 0.6, colors = colors, shadow = True, autopct = '%1.1f %%', pctdistance = 1.1, labeldistance = 1.2, startangle = 11, counterclock = False, wedgeprops = wedgeprops, textprops = textprops, explode = explode)

#Show Legend
p.legend()

#Display the Pie Chart
p.show()```

Output. Now, let’s understand the different arguments that we have passed to the `pyplot.pie()` method to make this pie chart.

• x (parts). The first argument is a Python List containing the numerical values using which you want to create the pie chart.
• labels. This is also a String-Type Python List containing the labels for each of the numerical shares in the same order as they are in the `parts` list.
• radius. It a numerical value that defines the radius of the pie chart circle.
• colors. Again, this is a python String-Type list containing the colors that you want to assign to each part of the pie-chart in order.
• shadow. It is a boolean type argument and if set to `True`, gives shadow to the wedges. Its default value is `False`.
• autopct. A String type argument to give formatted values to each of the parts. For example, `'%1.1f %%'` will display the percentages upto 1 decimal.
• pctdistance. It is the distance from the centre of each of the pie-slices to the starting of the `autopct` generated text.
• labeldistance. It is the distance of the label text starting to the centre of each pie-slice.
• startangle. It rotates the pie-char counter-clockwise from x-axis by this much degrees.
• counterclockwise. If set to `False`, sets the fractions direction clockwise. By default, its value is `True`. Hence, the default fractions direction is counter-clockwise.
• wedgeprops. It is a Python Dictionary containing the properties for the wedges. You can check all of the wedge object properties here.
• textprops. It is also a Python Dictionary that contains the properties of the percentage text. Find all of the text properties here.
• explode. It is basically a Python List that contains radius fractions for each of the wedges that it has to be offset from the wedge.

Note. Among all of these parameters, only the x or the parts parameter is necessary to be passed as an argument while all of the other parameters are optional.

You can do a lot more using the `matplotlib.pyplot.pie()` method like creating the nested pie-charts. But that also requires the knowledge of some other modules like `numpy` for the sake of convenience. So, we’ll figure out that but in some other article.

I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!