Status
Not started
Text
sns.kdeplot()
Text 1
Kernel Density Estimate plot for smooth distribution curves.
sns.kdeplot()
is used to visualize the probability density function (PDF) of a continuous variable using Kernel Density Estimation (KDE). It provides a smooth alternative to histograms for understanding distribution shapes.
1. General Syntax
python
CopyEdit
sns.kdeplot(
data=None,
x=None,
y=None,
hue=None,
weights=None,
bw_method=None,
bw_adjust=1,
fill=False,
common_norm=True,
common_grid=False,
cumulative=False,
cut=3,
clip=None,
gridsize=200,
legend=True,
palette=None,
alpha=None,
linewidth=None,
linestyle=None,
thresh=0.05
)
2. Key Parameters
Parameter | Description |
x , y | Input data for univariate or bivariate KDE. |
hue | Grouping variable that will produce separate curves. |
bw_adjust | Adjusts the bandwidth (smoothing); lower = more detail. |
fill | Fills the area under the KDE curve. |
cumulative | If True , shows the cumulative distribution. |
common_norm | If False , normalizes each subgroup independently. |
cut | Extends the curve beyond extreme data points (default is 3). |
clip | Tuple to limit the KDE support range. |
gridsize | Number of points used to evaluate the KDE curve. |
3. Dataset Setup
We’ll use the penguins
dataset from Seaborn.
python
CopyEdit
import seaborn as sns
import matplotlib.pyplot as plt
# Load dataset
data = sns.load_dataset("penguins")
data.head()
Dataset Columns
- species: Penguin species.
- bill_length_mm: Length of the bill.
- bill_depth_mm: Depth of the bill.
- flipper_length_mm: Flipper length.
- body_mass_g: Body mass.
- sex: Gender.
4. Basic KDE Plot
Plot distribution of bill length
sns.kdeplot(data=data, x="bill_length_mm")
plt.title("KDE Plot of Bill Length")
plt.show()
✅ Explanation
- Shows a smooth distribution curve for
bill_length_mm
.
5. KDE Plot with Shaded Area (fill=True
)
python
CopyEdit
sns.kdeplot(data=data, x="bill_length_mm", fill=True)
plt.title("Filled KDE Plot of Bill Length")
plt.show()
✅ Explanation
- The area under the curve is shaded, making the shape more visible.
6. Grouping by Category (hue
)
python
CopyEdit
sns.kdeplot(data=data, x="bill_length_mm", hue="species", fill=True)
plt.title("KDE Plot of Bill Length by Species")
plt.show()
✅ Explanation
- Shows separate KDE curves for each penguin species.
7. Adjusting Smoothing (bw_adjust
)
python
CopyEdit
sns.kdeplot(data=data, x="bill_length_mm", bw_adjust=0.5, fill=True)
plt.title("KDE Plot with Lower Bandwidth (More Detail)")
plt.show()
python
CopyEdit
sns.kdeplot(data=data, x="bill_length_mm", bw_adjust=2, fill=True)
plt.title("KDE Plot with Higher Bandwidth (More Smoothing)")
plt.show()
✅ Explanation
- Lower
bw_adjust
= more peaks and valleys (may overfit). - Higher
bw_adjust
= smoother curve (may oversmooth).
8. Cumulative KDE Plot
python
CopyEdit
sns.kdeplot(data=data, x="bill_length_mm", cumulative=True)
plt.title("Cumulative KDE Plot of Bill Length")
plt.show()
✅ Explanation
- Shows the cumulative distribution (like a CDF).
9. Clipping the Curve
python
CopyEdit
sns.kdeplot(data=data, x="bill_length_mm", clip=(35, 50))
plt.title("KDE Plot with Clipping Range (35–50 mm)")
plt.show()
✅ Explanation
- Restricts the x-axis range of the KDE.
10. Horizontal KDE
python
CopyEdit
sns.kdeplot(data=data, y="bill_length_mm", fill=True)
plt.title("Horizontal KDE Plot")
plt.show()
✅ Explanation
- Same KDE curve, but along the y-axis.
11. Customizing Appearance
python
CopyEdit
sns.kdeplot(
data=data,
x="bill_length_mm",
hue="species",
fill=True,
alpha=0.4,
linewidth=2,
linestyle="--"
)
plt.title("Customized KDE Plot")
plt.show()
✅ Explanation
- Custom
alpha
for transparency,linewidth
, andlinestyle
.
12. 2D KDE Plot (Bivariate)
python
CopyEdit
sns.kdeplot(data=data, x="bill_length_mm", y="bill_depth_mm", fill=True, cmap="Blues")
plt.title("2D KDE Plot of Bill Length and Depth")
plt.show()
✅ Explanation
- Shows density contours for joint distribution of two variables.
13. 2D KDE with Hue
python
CopyEdit
sns.kdeplot(
data=data,
x="bill_length_mm",
y="bill_depth_mm",
hue="species",
fill=True
)
plt.title("2D KDE by Species")
plt.show()
✅ Explanation
- One KDE surface per species, filled with different colors.
14. Final Example: Fully Customized KDE
python
CopyEdit
plt.figure(figsize=(10, 6))
sns.kdeplot(
data=data,
x="bill_length_mm",
hue="species",
fill=True,
bw_adjust=1.2,
alpha=0.5,
linewidth=2,
palette="Set2"
)
plt.title("Smooth KDE Plot of Bill Length by Species")
plt.grid(axis="y", linestyle="--", alpha=0.6)
plt.show()
✅ Features in This Plot:
- Custom bandwidth
- Shaded curves
- Hue separation by species
- Larger plot size
- Light grid for clarity
Conclusion
✅ sns.kdeplot()
is excellent for:
- Exploring distribution shapes
- Visualizing group-wise densities
- Creating 2D smoothed plots
✅ It's a smooth and flexible alternative to histograms and is especially useful for probabilistic insights in exploratory data analysis.
Master KDE plots to elevate your distribution analysis in a visually elegant way! 🚀