You can better visualize a pandas series with categorical values via a pie chart of counts. In this tutorial, we will look at how to plot a pie chart of pandas series values.

## Pandas Series as Pie Chart

To plot a pie chart, you first need to create a series of counts of each unique value (use the pandas `value_counts()`

function) and then proceed to plot the resulting series of counts as a pie chart using the pandas series `plot()`

function.

The `plot()`

function plots a line chart of the series values by default but you can specify the type of chart to plot using the `kind`

parameter. To plot a pie chart, pass `'pie'`

to the `kind`

parameter. The following is the syntax:

# pie chart using pandas series plot() s.value_counts().plot(kind='pie')

Here, s is the pandas series with categorical values which is converted to a series of counts using the `value_counts()`

function. The pandas series `plot()`

function returns a matplotlib axes object to which you can add additional formatting.

## Examples

Let’s look at some examples of plotting a series values as a pie chart. First, we’ll create a sample pandas series which we will be using throughout this tutorial.

import pandas as pd # pandas series Wimbledon winners from 2015 to 2019 wimbledon_winners = pd.Series(index=[2015, 2016, 2017, 2018, 2019], data=['Novak Djokovic', 'Andy Murray', 'Roger Federer', 'Novak Djokovic', 'Novak Djokovic'], name='Winners') # display the series print(wimbledon_winners)

Output:

2015 Novak Djokovic 2016 Andy Murray 2017 Roger Federer 2018 Novak Djokovic 2019 Novak Djokovic Name: Winners, dtype: object

You can see the contents of the series object above. We now have a pandas series containing the name of Wimbledon Winners from 2015 to 2019 with the year as its index.

Let’s create a series of counts of championships won by each player during the period using the `value_counts()`

function. We’ll be using this series to plot our pie chart.

# series of counts wimbledon_wins_count = wimbledon_winners.value_counts() # print the counts print(wimbledon_wins_count)

Output:

Novak Djokovic 3 Andy Murray 1 Roger Federer 1 Name: Winners, dtype: int64

The returned series contains counts of victories by each player in the original series.

### 1. Plot Pie Chart of Series Values

To create a pie chart from the series values we’ll pass `kind='pie'`

to the pandas series `plot()`

function. For example, let’s see its usage on the “wimbledon_wins_count” series created above.

wimbledon_wins_count.plot(kind='pie')

Output:

The above pie chart shows the distribution of Wimbledon victories from 2015 to 2019. You can see that Novak Djokovic has won more than half of the championships during that period. Note that the resulting plot is a matplotlib pie chart.

For more on the pandas series plot() function, refer to its documentation.

### 2. Customize the plot formatting

You can also customize the formatting of the chart. For instance, you can add the axes labels, chart title, change colors and fonts, etc. Since the returned plot is a matplotlib axes object, you can apply any formatting that would work with matplotlib charts.

Let’s go ahead and add a title to our plot.

# create the pie chart ax = wimbledon_wins_count.plot(kind='pie') # set the title ax.set_title("Distribution of Wimbledon Victories 2015-2019")

Output:

You can see in the above chart has “Distribution of Wimbledon Victories 2015-2019” as its title.

For more on pie charts and their formatting in matplotlib, refer to our tutorial on matplotlib pie charts.

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 pandas version 1.0.5

**Subscribe to our newsletter for more informative guides and tutorials. ****We do not spam and you can opt out any time.**

Tutorials on pandas series –

- Convert Pandas Series to a DataFrame
- Convert Pandas Series to a List
- Convert Pandas Series to a NumPy Array
- Convert Pandas Series to a Dictionary
- Sort a Pandas Series
- Append Two Pandas Series
- Apply a Function to a Pandas Series
- Pandas – Shift column values up or down
- Plot a Histogram of Pandas Series Values
- Create a Pie Chart of Pandas Series Values
- Plot a Bar Chart of Pandas Series Values
- Create a Boxplot from Pandas Series Values
- Create a Density Plot from Pandas Series Values