How to Plot a Histogram Using Pandas?

In Pandas one of the visualization plot is Histograms are 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 DataFrames Series is on a similar scale.

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

1. Quick Examples of Pandas Histogram

If you are in hurry below are some quick examples of pandas Histogram.


# Following are the quick examples
# Example 1: Plot the histogram from DataFrame
df.hist()

# Example 2: Customize the bins of histogram
df.hist(bins = 3)

# example 3: create histogram of specified column
df.hist(column = 'Maths')

# Example 4: Plot the histogram using plot()
df.plot(kind = 'hist')

# Example 5: create histogram with title
df.plot(kind = 'hist', title = 'Students Marks')

# Example 6: Create multiple titles of histogram
 df.plot(kind='hist', subplots=True, title=['Maths', 'Physics', 'Chemistry'])

# Example 7: Create histogram using plot.hist()
df.plot.hist()

2. How to Plot Pandas Histogram

In Pandas a histogram is a graphical representation of data points, it can be organized into bins. Following are the multiple ways to make a histogram plot in pandas.

  • pd.DataFrame.hist(column)
  • pd.DataFrame.plot(kind='hist')
  • pd.DataFrame.plot.hist()

3. Plot Histogram Use hist() in pandas

Create a histogram using pandas hist() method, is a default method. For that we need to create Pandas DataFrame using Python Dictionary. Let’s create DataFrame.


# Create Pandas DataFrame
import pandas as pd
import numpy as np
# Create DataFrame
df = pd.DataFrame({
    'Maths': [80.4, 50.6, 70.4, 50.2, 80.9],
    'Physics': [70.4, 50.4, 60.4, 90.1, 90.1],
    'Chemistry': [40, 60.5, 70.8, 90.88, 40],
    'Students': ['Student1', 'Student1', 'Student1', 'Student2', 'Student2']
})
print(df)

Yields below output.


# Output:
  Maths  Physics  Chemistry  Students
0   80.4     70.4      40.00  Student1
1   50.6     50.4      60.50  Student1
2   70.4     60.4      70.80  Student1
3   50.2     90.1      90.88  Student2
4   80.9     90.1      40.00  Student2

In order to plot a histogram in pandas using hist() function, DataFrame can call the hist(). It will return the histogram of each numeric column in the pandas DataFrame.


# Plot the histogram from DataFrame
df.hist()

Yields below output.

Plot histogram in pandas
Plot the histogram using hist() function.

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 which we want in the histogram.


# Customize the bins of histogram
df.hist(bins = 3)
Pandas histogram

3.2 Plot a histogram For Specific Column.

As we know from the above, by default, we can get a histogram for each column of given DataFrame. If we want plot histogram on a specific column , then we can go with the column parameter of the hist()function. For, that we need to pass which column we want to plot the histogram into hist() function, it will plot the specified column histogram.


# create histogram of specified column
df.hist(column = 'Maths')
Pandas plot histogram

3.3 Plot Histograms for different groups along Specified column

In hist() function using by parameter we can plot separate histograms for different groups of data. For, that we have to specify which column groups we want to plot separate histograms. It will return separate histograms for each group.

For example, two histograms are created for the maths column.

Pandas plot histogram

4. Plot Histogram use plot() function

Histogram can also be created by using the plot() function on pandas DataFrame. The main difference between the .hist() and .plot() functions is that the hist() function creates histograms for all the numeric columns of the DataFrame on the same figure. No separate plots are made in the case of the .plot function.

Plot function can also take in the bins and by parameter same as hist function. The plot function can be used for histogram plotting in two ways

4.1 Syntax of plot()

Following is the syntax of plot() function.


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

kind :  It takes in the kind of plot to be created. For histogram, you need to pass the value as hist.


# Plot the histogram using plot()
df.plot(kind = 'hist')
Pandas plot histogram

4.2 Create Title of Histogram

Using plot() function we are not able to construct histogram of all individual columns of DataFrame


# create histogram with title
df.plot(kind = 'hist', title = 'Students Marks')
Pandas plot title
Histogram using pandas

4.3 Create Multiple Titles for Individual Subplots

The following code shows how to create individual titles for subplots in pandas:


# Create multiple titles of histogram
 df.plot(kind='hist', subplots=True, title=['Maths', 'Physics', 'Chemistry'])
Pandas plot title
Individual columns of a histogram

5. Create Histogram use plot.hist() function

Use plot.hist() function we can find out the histogram of DataFrame. Directly access the histogram hist method from the plot function. Just add the .hist() after .plot function.

5.1 Syntax of Pandas plot.hist()

Following is the syntax of plot.hist().


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

5.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()
df.plot.hist()

6. Conclusion

In this article, I have explained concept of histogram and using various hist() functions how we can plot the histogram from the DataFrame.

Happy learning !!

Related Articles

References

Leave a Reply