You can better visualize a pandas series with categorical values via a bar plot of counts. In this tutorial, we will look at how to plot a bar chart of pandas series values.
Pandas Series as Bar Chart
For a bar 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 using the pandas series
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 bar chart, pass ‘bar’ to the
kind parameter. The following is the syntax:
# bar chart using pandas series plot() s.value_counts().plot(kind='bar')
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.
Let’s look at some examples of plotting a pandas series values as a bar 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)
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 count of championships won by each player during the period.
# series of counts wimbledon_wins_count = wimbledon_winners.value_counts() # print the counts print(wimbledon_wins_count)
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. Bar Chart Chart of Series Values
To create a bar chart from the series values we’ll pass
kind='bar' to the pandas series
plot() function. For example, let’s see its usage on the “wimbledon_wins_count” series created above.
The above bar chart shows the distribution of Wimbledon victories from 2015 to 2019. You can see that Novak Djokovic has won 3 championships during that period while Andy Murray and Roger Federer have won 1 each. Note that the resulting plot is a matplotlib bar 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 plots.
Let’s go ahead and rotate the xtick labels, add a label for the y-axis, and add a title to our plot.
# create the bar plot ax = wimbledon_wins_count.plot(kind='bar') # rotate xtick labels ax.set_xticklabels(ax.get_xticklabels(), rotation=0) # set the y-axis label ax.set_ylabel("Victories") # set the title ax.set_title("Distribution of Wimbledon Victories 2015-2019")
The above chart now has the xtick labels placed horizontally, “Victories” as its y-axis label, and “Distribution of Wimbledon Victories 2015-2019” as its title.
For more on bar charts and their formatting in matplotlib, refer to our tutorial on matplotlib bar 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