Adding rugplot on top of a histogram or KDE highlights where each observation lies, making the distribution easier to read.
import seaborn as sns
import matplotlib.pyplot as plt
diamonds = sns.load_dataset("diamonds").sample(300, random_state=0)
fig, ax = plt.subplots(figsize=(6, 3.5))
sns.kdeplot(data=diamonds, x="price", ax=ax, color="#0ea5e9")
sns.rugplot(data=diamonds, x="price", ax=ax, color="#1d4ed8", alpha=0.4)
ax.set_xlabel("Price ($)")
ax.set_ylabel("Density")
ax.set_title("Diamond prices: KDE + rug plot")
ax.grid(alpha=0.2)
fig.tight_layout()
fig.savefig("static/images/visualize/distribution/rugplot.svg")
Reading tips #
- Dense clusters of short rug marks indicate many observations in that range.
- Use a light color so the rug does not dominate the KDE.
- On very large data sets, rug plots can be expensive to draw; consider sampling or reducing the
height.