Pandas.DataFrame.hist() function is useful in understanding the distribution of numeric variables. This function splits up the values into the numeric variables. Its main functionality is to make the Histogram of a given Data frame.
The distribution of data is represented by Histogram. When Function Pandas DataFrame.hist() is used, it automatically calls the function matplotlib.pyplot.hist() on each series in the DataFrame. As the result, we obtained one histogram per column.
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
Example 1: Creating Histograms of 2 columns of Pandas data frame
Sometimes we need to plot Histograms of columns of Data frame in order to analyze them more deeply. In that case, dataframe.hist() function helps a lot. Using this function, we can plot histograms of as many columns as we want.
Python3
# Importing pandas library import pandas as pd # Creating a Data frame 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 ] }) # Creating Histograms of columns 'Length' # and 'Breadth' using Dataframe.hist() # function hist = values.hist(bins = 5 ) |
Output:
In the above example, we plot histograms of columns ‘Length’ and ‘Breadth’ using dataframe.hist() function.
Example 2: Creating Histograms of 3 columns of Pandas data frame
Python3
# Importing pandas library import pandas as pd # Creating a Data frame 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 ]}) # Creating Histograms of columns 'Length', # 'Breadth' and 'Height' using Dataframe.hist() # function hist = values.hist(bins = 12 ) |
Output:
In the above example, we plot histograms of columns ‘Length‘, ‘Breadth‘ and ‘Height‘ using dataframe.hist() function.
Example 3: Creating Histograms of 4 columns of Pandas data frame
Python3
# Importing pandas library import pandas as pd # Creating a Data frame 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 ] }) # Creating Histograms of columns 'Length', # 'Breadth', 'Height' and 'Weight' # using Dataframe.hist() function hist = values.hist(bins = 8 ) |
Output:
In the above example, we plot histograms of columns ‘Length‘, ‘Breadth‘, ‘Height‘ and ‘Weight’ using dataframe.hist() function.