• Post author:
  • Post category:Pandas
  • Post last modified:May 27, 2024
  • Reading time:15 mins read
You are currently viewing How to Make a Histogram in Pandas Series?
Pandas Series Histogram

In Pandas, the plot() function is used to make a histogram from a given Series. Histograms are a type of visualization in Pandas that represent the frequency distribution of numerical data. They divide the values of a numerical variable into bins and count how many values fall into each bin. Creating a histogram is an effective way to explore the distribution of your data, especially when the Series values are on a similar scale. We can also create histogram plot using Pandas DataFrame.

Advertisements

In this article, I will explain the concept of the histogram and using different histogram functions how we can plot the histogram from the given Series.

Note: You can use the Pandas Series plot() function to create various types of plots such as bar charts and pie charts by passing the appropriate value to the kind parameter, with ‘line’ being the default value.

Following are the multiple ways to make a histogram plot in pandas.

  • pd.Series.plot(kind='hist')
  • pd.Series.hist()
  • pd.Series.plot.hist()

Quick Examples of Pandas Series Histogram

If you are in a hurry, below are some quick examples of Pandas Series histograms.


# Quick examples of pandas series histogram

# Example 1: create a histogram of Series 
# Using plot()
ser.plot(kind = 'hist')

# Example 2: Customize the histogram
plt.xlabel("Marks",  size = 20)
plt.title("Marks of the Students", size = 25)

# Example 3: Using hist() plot a histogram of Series
ser.hist()

# Example 4: Customize the bins of hist()
ser.hist(bins = 3)

# Example 5: Create histogram using plot.hist()
ser.plot.hist()

Pandas Series Plot() Introduction

Following is the syntax of plot() method.


# Syntax of plot()
Series.plot(kind='hist')

kind :  For the histogram, you need to pass the value as hist.

Usage of plot()

The Python Pandas library primarily focuses on data analysis but can also be used for basic data visualization. For exploratory data analysis, Pandas is highly useful and practical, offering the plot() function and several other wrapper functions for visualizing data.

To run some examples of how to make a histogram in the pandas series, let’s create pandas series using list of values.


# Create dataframe
# Create Pandas dataframe
import pandas as pd
# Create DataFrame
ser = pd.Series([80.4, 50.6, 70.4, 50.2, 80.5, 70.4, 50.4, 60.4, 90.1, 90.5], name = 'Marks')
print(ser)

Yields below output.

Pandas Series histogram
Pandas Series

Plot Histogram of Series Values

To create a histogram from the series values we will pass kind='hist' into the pandas series plot() function. For example, let’s see how the Series values distribute in a histogram,


# Create a histogram of Series using plot()
ser.plot(kind = 'hist')

Yields below output.

Pandas Series hist
Pandas Series histogram

As we can see from the above histogram, it shows that the frequency distribution of Series values from those '50' marks got more frequency than others.

Customize the Histogram

We can customize the histogram by adding the title of the histogram, Labeling of the axis is done by using the Matplotlib object imported from pyplot. By using some of the keyword arguments(like font, color, etc.) of plot() function we can also customize the histogram of Series.


# Customize the histogram
import matplotlib.pyplot as plot
plt.xlabel("Marks",  size = 20)
plt.title("Marks of the Students", size = 25)

Yields the same output as above.

Pandas Series hist
Pandas Series histogram

Pandas Series Histogram Using hist()

Alternatively, we can also create a histogram for the values using Series.hist() function. It will distribute the given Series values into bins. Default bin value is 10.For example,


# Using hist() plot a histogram of Series
ser.hist()

Yields the same output as above.

Pandas Series histogram

Bins of a histogram

In histogram, bins are the class intervals in which our data is grouped. We can create a plot based on the number of values in each interval. By default, the hist() function takes 10 bins. We can customize the number of bins using this function. We can Pass the number of bins directly that we want in the histogram.


# Customize the bins of hist()
ser.hist(bins = 3)

Yields the same output as above.

Pandas Series hist
Pandas Series histogram

Pandas Series Histogram Using Series.plot.hist()

Finally, use Ser.plot.hist() function to get the histogram of Pandas Series. Directly access the histogram hist method from the plot function.

Syntax of Pandas plot.hist()

Following is the syntax of plot.hist().


# Syntax of plot.hist()
Series.plot.hist(by=None, bins=10, **kwargs)

Parameters of the plot.hist()

  • by : (str or sequence, optional)Column in the DataFrame to group by.
  • bin : (int, default 10)Number of histogram bins to be used.
  • **kwargs : Additional keyword arguments 

# Create histogram using plot.hist()
ser.plot.hist()
Pandas Series histogram

Frequently Asked Questions on Make a Histogram in Series

How do I make a histogram in Pandas Series?

You can make a histogram in Pandas Series using the .hist() method. This method plots the frequency distribution of the data.

What parameters can I adjust in the .hist() method?

Some common parameters you can adjust include bins, color, alpha, grid, xlabel, ylabel, and title. These parameters allow you to customize the appearance of the histogram.

How do I specify the number of bins in the histogram?

You can specify the number of bins using the bins parameter. For example, bins=10 will create 10 equally spaced bins for the histogram.

Is it possible to add grid lines to the histogram?

To add grid lines using the grid parameter. Set grid=True to display grid lines and grid=False to hide them

How do I label the x-axis and y-axis of the histogram?

You can label the x-axis and y-axis using the xlabel and ylabel parameters, respectively. Simply pass the desired labels as strings.

Conclusion

In this article, you have learned the concept of histograms and how to plot a histogram from a given Series using different functions. The functions I have explained in this article are pd.Series.plot(kind='hist'), pd.Series.hist(), and pd.Series.plot.hist()

Happy learning!!

Related Articles

References