Home » Python » How to Plot Histogram in Python

How to Plot Histogram in Python

What is Histogram Plot?

Histograms are very similar to bar graphs. Histogram organizes the data points into a range. It takes series of groups of data points into bins or ranges. To plot histogram in python use displot() function of seaborn library

In this article, I will explain you how to plot histogram in python using seaborn package and different histogram in python examples to customize it for better visualization.

Installation of Packages

We will need pandas and seaborn packages to plot histogram in python program. If you don’t have these packages installed on your system, install it using below commands.

pip install pandas
pip install seaborn

How to Plot Histogram in Python using seaborn

Let’s see an example to plot histogram chart in python using seaborn and pandas library.

Installation of Packages

We will need seaborn packages to show histogram. Install package using below command.

pip install seaborn

Import libraries

Import seaborn library in our python histogram code.

# Import library

import seaborn as sns

Cool Tip: Learn How to plot horizontal line graph in python !

Prepare dataset

Seaborn library has inbuilt datasets. We will use tips dataset to visualize histogram

  • Load tips dataset using load_dataset() function of seaborn library
# Load the tips dataset
df = sns.load_dataset('tips')

Plot Histogram using displot()

Use sns.displot() function of seaborn module to draw histogram, displot() function accepts dataframe object as input to plot histogram in python.

# Plot histogram 
sns.distplot( df["total_bill"] )

Histogram Python Code

Use below entire histogram python code using seaborn library

# Import library
import seaborn as sns

# Load the tips dataset
df = sns.load_dataset('tips')

# Plot histogram 
sns.displot( df["total_bill"] )

Histogram Python Visualization Output

Histogram in Python - Using Seaborn
Histogram in Python – Using Seaborn

Cool Tip: Learn How to plot Treemap chart in python !

Plot Python Histogram with Bins

Let’s see an example to plot histogram in python with bins.

Installation of Packages

We will need seaborn package to plot histogram. Install package using below command.

pip install seaborn

Import libraries

Import seaborn library in our python histogram code.

# Import library
import seaborn as sns

Prepare dataset

Seaborn library has inbuilt datasets. We will use tips dataset to visualize histogram

  • Load tips dataset using load_dataset() function of seaborn library
# Load the tips dataset
df = sns.load_dataset('tips')

Plot Histogram using displot()

Use sns.displot() function of seaborn module to draw histogram, displot() function accepts dataframe object as input to plot histogram and bins = 10 to organize grouped data better and easy for visualization to detect the patterns.

# Control the number of bins and plot histogram
sns.distplot( df["total_bill"],bins=10 )

Histogram Python Code

Use below entire histogram python code using seaborn library

# Import library
import seaborn as sns

# Load the tips dataset
df = sns.load_dataset('tips')

# Control the number of bins and Plot histogram 
sns.displot( df["total_bill"],bins=10 )

Python Histogram with Bins Visualization Output

Python Histogram with Bins
Python Histogram with Bins

Cool Tip: Learn How to plot vertical subplot line graph in python !

Plot Python Histogram Vertical

Let’s see an example to plot histogram vertical in python.

Installation of Packages

We will need seaborn package to plot histogram. Install package using below command.

pip install seaborn

Import libraries

Import seaborn library in our python histogram code.

# Import library
import seaborn as sns

Prepare dataset

Seaborn library has inbuilt datasets. We will use tips dataset to visualize histogram

  • Load tips dataset using load_dataset() function of seaborn library
# Load the tips dataset
df = sns.load_dataset('tips')

Plot Histogram Vertical using histplot()

Use sns.histplot() function of seaborn module to draw histogram vertical.

histoplot() function accepts dataframe object as input to plot histogram vertical and y = “total_bill”

# Python Vertical histogram
sns.histplot(data=df, y="total_bill")

Python Histogram Vertical Code

Use below entire python histogram vertical code using seaborn library

# Import library and dataset
import seaborn as sns

# Load the tips dataset
df = sns.load_dataset('tips')
 
# Python Vertical histogram
sns.histplot(data=df, y="total_bill")

Python Histogram Vertical Visualization Output

Python Historical Vertical Chart
Python Histogram Vertical Chart

Cool Tip: Learn How to plot line graph in python !

Conclusion

I hope you found above article to plot histogram in python program using seaborn package informative and educational.

Use sns.displot() function of seaborn module to create histogram in python program.