pandas series plotted as a histogram

Plot a Histogram of Pandas Series Values

In this tutorial, we will look at how to plot a pandas series values as a histogram.

To plot a pandas series, you can use the pandas series plot() function. It 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 histogram, pass 'hist' to the kind paramter. The following is the syntax:

# histogram using pandas series plot()
s.plot(kind='hist')

Here, s is the pandas series you want to plot. 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 histogram. First, we’ll create a sample pandas series which we will be using throughout this tutorial.

import pandas as pd

# scores in the Math class
math_scores = pd.Series(data=[72, 41, 65, 63, 82, 63, 51, 57, 39, 63,
                           62, 68, 52, 76, 62, 73, 72, 73, 71, 62,
                           76, 53, 71, 79, 77, 35, 65, 59, 58, 70,
                           73, 69, 59, 75, 73, 63, 65, 81, 46, 59,
                           53, 71, 79, 80, 60, 60, 64, 40, 73, 75,
                           68, 58, 81, 65, 55, 62, 82, 47, 85, 62,
                           39, 77, 82, 78, 57, 58, 72, 75, 65, 68,
                           86, 49, 39, 64, 54, 68, 85, 77, 62, 53,
                           52, 76, 80, 84, 69, 61, 69, 65, 89, 97,
                           71, 61, 77, 40, 83, 52, 78, 54, 64, 58],
                        name='Scores')

# display the series head
print(math_scores.head())

Output:

0    72
1    41
2    65
3    63
4    82
Name: Scores, dtype: int64

You can see the top five values of the series object above. We now have a pandas series containing the scores of students in a Math class.

To create a histogram from the series values we’ll pass kind='hist' to the pandas series plot() function. For example, let’s see its usage on the “math_scores” series created above.

math_scores.plot(kind='hist')

Output:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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.

Histogram of values from the "math_scores" series

The above histogram show that a large number of students got scores between 60 to 80. Note that the resulting plot is a matplotlib histogram chart.

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

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 the x-axis label and title to our plot.

# create the histogram
ax = math_scores.plot(kind='hist')
# set the x-axis label
ax.set_xlabel("Scores")
# set the title
ax.set_title("Distribution of Math Scores of the Class")

Output:

Histogram from pandas series with added formatting.

You can see in the above chart has “Scores” as its x-axis label, and “Distribution of Math Scores of the Class” as its title.

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

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 –

Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top