0
0
Data Analysis Pythondata~5 mins

Distribution plots (histplot, kdeplot) in Data Analysis Python

Choose your learning style9 modes available
Introduction

Distribution plots help us see how data values spread out or cluster. They make it easy to understand patterns in numbers.

To check how exam scores are spread among students.
To see the distribution of daily temperatures in a city.
To understand customer ages in a store.
To find out if data is mostly low, high, or balanced.
To compare two groups’ data shapes visually.
Syntax
Data Analysis Python
import seaborn as sns
sns.histplot(data, bins=number_of_bins, kde=True)
sns.kdeplot(data, shade=True)

histplot draws bars showing counts in ranges (bins).

kdeplot draws a smooth curve estimating data density.

Examples
This shows how many times each number appears using bars.
Data Analysis Python
import seaborn as sns
import matplotlib.pyplot as plt

# Simple histogram
sns.histplot([1, 2, 2, 3, 3, 3, 4])
plt.show()
Bars show counts and the smooth line shows data density.
Data Analysis Python
import seaborn as sns
import matplotlib.pyplot as plt

# Histogram with KDE curve
sns.histplot([1, 2, 2, 3, 3, 3, 4], kde=True)
plt.show()
Shows a smooth curve estimating where data points cluster.
Data Analysis Python
import seaborn as sns
import matplotlib.pyplot as plt

# KDE plot only
sns.kdeplot([1, 2, 2, 3, 3, 3, 4], shade=True)
plt.show()
Sample Program

This program makes 1000 random numbers centered at 0. It then shows a histogram with bars and a smooth KDE curve to see the data spread.

Data Analysis Python
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

# Create sample data: 1000 random numbers from normal distribution
data = np.random.normal(loc=0, scale=1, size=1000)

# Plot histogram with KDE
sns.histplot(data, bins=30, kde=True)
plt.title('Histogram with KDE')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.show()
OutputSuccess
Important Notes

Use bins to control how many bars appear in the histogram.

Adding kde=True in histplot overlays the smooth curve.

shade=True in kdeplot fills the area under the curve for better visibility.

Summary

Distribution plots show how data values spread or cluster.

histplot uses bars; kdeplot uses smooth curves.

They help understand data shape and compare groups visually.