Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีสร้าง Grouped Boxplot ด้วย Matplotlib และ Seaborn

ภาพรวม

Grouped Boxplot แสดงการกระจายของข้อมูลแยกตามกลุ่ม (เช่น เพศ) ในแต่ละหมวดหมู่ (เช่น วัน) ใช้ Seaborn ทำได้ง่ายมาก

ขั้นตอนการทำงาน

  • Import seaborn และ matplotlib.pyplot
  • กำหนดขนาดฟิกเกอร์
  • โหลดตัวอย่างข้อมูล tips จาก Seaborn
  • ใช้ sns.boxplot() กำหนด x, y และ hue
  • แสดงผล

ตัวอย่างโค้ด

import seaborn as sns
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

data = sns.load_dataset('tips')

sns.boxplot(x=data['day'], y=data['total_bill'], hue=data['sex'])
plt.show()

ผลลัพธ์

Boxplot แยกวัน (Thur, Fri, Sat, Sun) และแยกเพศ (Male/Female) ด้วยสี

วิธีสร้าง Grouped Boxplot ด้วย Matplotlib และ Seaborn วิธีสร้าง Grouped Boxplot ด้วย Matplotlib และ Seaborn

คำอธิบายพารามิเตอร์

  • x – หมวดหมู่หลัก (วัน)
  • y – ค่าตัวเลข (total_bill)
  • hue – กลุ่มย่อย (sex) จะแสดงเป็น box ติดกัน
  • Seaborn คำนวณ quartiles, median, whiskers, outliers ให้อัตโนมัติ