Seaborn Distribution Plots
How is a variable distributed?
๐ histplot
sns.histplot(data=df, x="total_bill")
sns.histplot(data=df, x="total_bill", bins=30)
sns.histplot(data=df, x="total_bill", hue="time") # overlaid histograms by category
sns.histplot(data=df, x="total_bill", hue="time", multiple="stack") # stacked instead of overlaid
sns.histplot(data=df, x="total_bill", kde=True) # add a KDE curve on top
sns.histplot(data=df, x="total_bill", stat="density") # normalize to density instead of countmultiple= | Effect (with hue) |
|---|---|
"layer" (default) | overlapping, semi-transparent |
"stack" | stacked bars |
"dodge" | side-by-side bars |
"fill" | stacked, normalized to 100% |
๐ kdeplot โ smoothed density curve
sns.kdeplot(data=df, x="total_bill")
sns.kdeplot(data=df, x="total_bill", hue="time")
sns.kdeplot(data=df, x="total_bill", hue="time", fill=True) # shaded area under curve
sns.kdeplot(data=df, x="total_bill", y="tip") # 2D density (contour plot)KDE = smoothed histogram. Good for comparing shapes of distributions across groups without bin-width artifacts.
๐ถ ecdfplot โ cumulative distribution
sns.ecdfplot(data=df, x="total_bill")
sns.ecdfplot(data=df, x="total_bill", hue="time")ECDF shows "what % of data is โค x" โ no binning choices needed, often clearer than a histogram for comparing distributions.
๐ rugplot โ raw data ticks along an axis
sns.rugplot(data=df, x="total_bill")
sns.histplot(data=df, x="total_bill")
sns.rugplot(data=df, x="total_bill") # combine: histogram + individual data point ticks๐ฏ displot โ figure-level wrapper
sns.displot(data=df, x="total_bill", kind="hist")
sns.displot(data=df, x="total_bill", kind="kde")
sns.displot(data=df, x="total_bill", kind="ecdf")
sns.displot(data=df, x="total_bill", col="time", kind="hist") # facet by category๐งฎ Choosing which one
| Question | Use |
|---|---|
| Shape + counts, simple | histplot |
| Smooth shape comparison across groups | kdeplot |
| โWhat fraction is below Xโ | ecdfplot |
| Faceted grid of distributions | displot |