Facet plots ใน seaborn
ฟังก์ชันการพล็อตบางอย่างใน seaborn เช่น histplot() และ lmplot() มีการสร้าง facet ในตัวอยู่แล้ว เพียงแค่ส่งอาร์กิวเมนต์ col และ/หรือ row ก็สามารถสร้าง facet ในกราฟได้ทันที
สำหรับฟังก์ชันที่ไม่มี facet ในตัว สามารถสร้างขึ้นเองด้วยฟังก์ชัน FacetGrid() แล้วระบุ col และ/หรือ row เพื่อแบ่ง facet โดยใช้โค้ดดังนี้
import seaborn as sns
import matplotlib.pyplot as plt
# Create a facet
facet = sns.FacetGrid(df, col='column_a', row='column_b')
# Generate a facetted scatter plot
facet.map(plt.scatter, 'column_x', 'column_y')
plt.show()
สามารถเพิ่มมิติข้อมูลอีกชั้นลงในกราฟได้โดยใช้อาร์กิวเมนต์ hue เพื่อแยกสีจุดตามตัวแปรที่ต้องการ
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Python สำหรับผู้ใช้ R
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
import seaborn as sns
import matplotlib.pyplot as plt
# Scatter plot of total_bill and tip faceted by smoker and colored by sex
sns.lmplot(x=____, y=____, data=tips, hue=____, col=____)
plt.show()