Search⌘ K
AI Features

Hist Plots

Explore how to create univariate and bivariate histogram plots using Seaborn. Learn to customize bins, apply KDE overlays, and handle skewed data with log scales. Understand how to use hue for categorical distinctions and enhance histograms with various styling options to better interpret data distributions.

Overview

Hist plot stands for histogram plot. It represents the distribution of continuous numerical data. Bar plots display the frequency of different categorical variables, whereas histograms show the frequency of numeric variables in the form of bins.

Univariate hist plot

Let’s get started by importing the required libraries. We’ll also import the tips and diamonds datasets and use them for our visualizations.

Python
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme()
# import datasets
tips_df = sns.load_dataset("tips")
diamond_df = sns.load_dataset("diamonds")
print(tips_df.head())
print("\n")
print("\n")
print(diamond_df.head())

We plot a histogram for the variable total_bill using the sns.histplot() function. The x-axis shows the range of values of total_bill, which is $10–50, and the y-axis shows the frequency of occurrence. Most of the total_bill observations are between $10 and $30.

Python
sns.histplot(x = 'total_bill', data = tips_df) # histogram
plt.ylabel('frequency')
plt.savefig('output/graph.png')

By default, seaborn determines the number of bins for the histograms based on the dataset. However, we can customize the number of bins in a histogram using the bins parameter. Be careful with the number of bins used because a small number may not show the patterns, and a huge number can add too much noise.

Python
sns.histplot(x = 'total_bill', bins= 30, data = tips_df) #customise no. of bins
plt.ylabel('frequency')
plt.savefig('output/graph.png')

We can ...