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

Pandas Series.plot() function is used to make a histogram of given series. In Pandas one of the visualization plot is Histograms and it is used to represent the frequency distribution for numeric data. It divides the values within a numerical variable into bins and counts the values that are fallen into a bin. Plotting a histogram is a good way to explore the distribution of our data. This is useful when the Series is 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 plot other plots like bar charts, pie charts, etc. For this, pass the suitable value to the kind parameter in which ‘line’ is 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()

1. Quick Examples of Pandas Series Histogram

If you are in hurry below are some quick examples of Pandas Series histogram.


# Below are some quick examples

# 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()

2. Syntax of Plot() function.

Following is the syntax of plot() function.


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

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

2.1 Usage of plot()

Python Pandas library is mainly focused on data analysis and it can also be used for data visualization to create basic plots. When we want to create exploratory data analysis plots, pandas are highly useful and practical. It provides plot() and several other wrapper functions for visualizing our data.

Now, 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

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

2.3 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

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

3.1 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

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

4.1 Syntax of Pandas plot.hist()

Following is the syntax of plot.hist().


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

4.2 Parameters of the plot.hist()

Following are the 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.

Can I change the color of the histogram bars?

You can change the color using the color parameter. You can specify a color name (e.g., 'blue', 'red') or a hexadecimal color code (e.g., '#FF5733').

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, I have explained the concept of the histogram and using different histogram functions how we can plot the histogram from the given Series. 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

Vijetha

Vijetha is an experienced technical writer with a strong command of various programming languages. She has had the opportunity to work extensively with a diverse range of technologies, including Python, Pandas, NumPy, and R. Throughout her career, Vijetha has consistently exhibited a remarkable ability to comprehend intricate technical details and adeptly translate them into accessible and understandable materials. Follow me at Linkedin.