Pandas DataFrame hist() Method | Create Histogram in Pandas
Last Updated :
01 Feb, 2024
A histogram is a graphical representation of the numerical data. Sometimes you’ll want to share data insights with someone, and using graphical representations has become the industry standard.
Pandas.DataFrame.hist() function plots the histogram of a given Data frame.
It is useful in understanding the distribution of numeric variables. This function splits up the values into the numeric variables.
When Pandas function DataFrame.hist() is used, it automatically calls the function matplotlib.pyplot.hist() on each series in the Pandas DataFrame. As a result, we obtain one histogram per column.
Syntax
Syntax: DataFrame.hist(data, column=None, by=None, grid=True, xlabelsize=None, xrot=None, ylabelsize=None, yrot=None, ax=None, sharex=False, sharey=False, figsize=None, layout=None, bins=10, backend=None, legend=False, **kwargs)
Parameters:
- data: DataFrame
- column: str or sequence
- xlabelsize: int, default None
- ylabelsize: int, default None
- ax: Matplotlib axes object, default None
- **kwargs All other plotting keyword arguments to be passed to matplotlib.pyplot.hist().
Return: matplotlib.AxesSubplot or numpy.ndarray
Examples
Below are the examples of Pandas DataFrame.hist() functions that we can use to plot histograms with Pandas in Python:
Example 1: Creating Histograms of 2 Columns of Pandas DataFrameÂ
Sometimes we need to plot Histograms of columns of DataFrame to analyze them more deeply. In that case, the DataFrame.hist() function helps a lot. Using this function, we can plot histograms of as many columns as we want.Â
In the below example, we plot histograms of columns ‘Length’ and ‘Breadth’ using the DataFrame.hist() function.
Python3
import pandas as pd
values = pd.DataFrame({
'Length' : [ 2.7 , 8.7 , 3.4 , 2.4 , 1.9 ],
'Breadth' : [ 4.24 , 2.67 , 7.6 , 7.1 , 4.9 ]
})
hist = values.hist(bins = 5 )
|
Output
Example 2: Plot Histogram With Pandas of 3 Columns
In the below example, we plot histograms of columns ‘Length‘, ‘Breadth‘, and ‘Height‘ using DataFrame.hist() function.Â
In this example, histograms for the columns ‘Length’, ‘Breadth’, and ‘Height’ are generated from a DataFrame named ‘values’ using the DataFrame.hist() function with 12 bins each.
Python3
import pandas as pd
values = pd.DataFrame({
'Length' : [ 2.7 , 8.7 , 3.4 , 2.4 , 1.9 ],
'Breadth' : [ 4.24 , 2.67 , 7.6 , 7.1 , 4.9 ],
'Height' : [ 5.8 , 5.5 , 7.8 , 10.88 , 0.1 ]})
hist = values.hist(bins = 12 )
|
Output
Example 3: Plot Histogram of 4 columns Using Pandas DataFrame
In the below example, we plot histograms of columns ‘Length‘, ‘Breadth‘, ‘Height‘, and ‘Weight’ using DataFrame.hist() function.
Python3
import pandas as pd
values = pd.DataFrame({
'Length' : [ 2.7 , 8.7 , 3.4 , 2.4 , 1.9 ],
'Breadth' : [ 4.24 , 2.67 , 7.6 , 7.1 , 4.9 ],
'Height' : [ 5.8 , 5.5 , 7.8 , 10.88 , 0.1 ],
'Weight' : [ 20 , 40.8 , 55.8 , 7.2 , 48 ]
})
hist = values.hist(bins = 8 )
|
Output
Example 4: Plot Histogram With Pandas For Specific Column
In this example, a histogram is created for the ‘Length’ column of a DataFrame named ‘values’ using Matplotlib and Pandas. The histogram is customized with a title, x-axis label (‘Length’), and y-axis label (‘Frequency’), and then displayed using plt.show().
Python3
import pandas as pd
import matplotlib.pyplot as plt
values = pd.DataFrame({
'Length' : [ 2.7 , 8.7 , 3.4 , 2.4 , 1.9 ],
'Breadth' : [ 4.24 , 2.67 , 7.6 , 7.1 , 4.9 ],
'Height' : [ 5.8 , 5.5 , 7.8 , 10.88 , 0.1 ],
'Weight' : [ 20 , 40.8 , 55.8 , 7.2 , 48 ]
})
hist = values[ 'Length' ].hist(bins = 8 )
plt.title( 'Histogram for Length Column' )
plt.xlabel( 'Length' )
plt.ylabel( 'Frequency' )
plt.show()
|
Output
Also Check:
Conclusion
Using graphs to graphically represent the findings is a very important step in data analysis. A histogram is used to represent the spread of your data.
In this tutorial, we covered how to use the in-built Pandas function DataFrame.hist() to plot a histogram in Python. We have explained the DataFrame.hist() function in easy words with examples. You can practice and experiment with the function to gain confidence using it.
Share your thoughts in the comments
Please Login to comment...