Pandas.DataFrame.hist() function in Python - GeeksforGeeks

01 Oct, 2020
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: DataFramecolumn: str or sequencexlabelsize: int, default Noneylabelsize: int, default Noneax: Matplotlib axes object, default None**kwargsAll 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 libraryimport pandas as pd  # Creating a Data framevalues = 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()# functionhist = 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 libraryimport pandas as pd  # Creating a Data framevalues = 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()# functionhist = 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 libraryimport pandas as pd  # Creating a Data framevalues = 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() functionhist = values.hist(bins=8)
Output:
In the above example, we plot histograms of columns ‘Length‘, ‘Breadth‘, ‘Height‘ and ‘Weight’ using dataframe.hist() function.
Python pandas-dataFrame
Python pandas-dataFrame-methods
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python String | replace()
Python program to convert a list to string
Create a Pandas DataFrame from Lists
Reading and Writing to text files in Python