Rugplot เป็นเส้นบอกตำแหน่งข้อมูล

Visualize

Rugplot เป็นเส้นบอกตำแหน่งข้อมูล

Created: Last updated: Read time: 1 min

เมื่อนำ rugplot ซ้อนบนฮิสโตแกรมหรือ KDE จะเห็นตำแหน่งของแต่ละจุดข้อมูลชัดขึ้นว่ามากระจุกตรงไหน

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("ราคา ($)")
ax.set_ylabel("ความหนาแน่น")
ax.set_title("KDE + Rugplot ของราคาพลอย")
ax.grid(alpha=0.2)

fig.tight_layout()

plt.show()

KDE และ rugplot ของราคาพลอย

วิธีอ่าน #

  • เส้นสั้นๆ ของ rugplot ที่เรียงถี่แสดงว่ามีจุดข้อมูลมากในช่วงนั้น
  • ใช้สีอ่อนเพื่อไม่ให้แย่งความสนใจจากเส้น KDE ที่เป็นกราฟหลัก
  • ถ้าจำนวนจุดเยอะมาก อาจต้องสุ่มมาบางส่วนหรือปรับ height ให้บางลงเพื่อลดภาระการเรนเดอร์