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

Python Pandas - จัดกลุ่ม Swarm Plot ตามตัวแปร Categorical ด้วย Seaborn

บทนำ

Swarm Plot ใน Seaborn เป็น Categorical Scatterplot ที่จุดไม่ทับซ้อน ใช้ seaborn.swarmplot() จัดกลุ่ม swarms โดยตั้งค่าตัวแปร Categorical เป็น x หรือ y

ขั้นตอน

  1. นำเข้าไลบรารี seaborn, pandas, matplotlib
  2. โหลดข้อมูลจาก Cricketers2.csv
  3. วาด Swarm Plot จัดกลุ่มตาม Role และ Age:
    sb.swarmplot(x=dataFrame["Role"], y=dataFrame["Age"])

โค้ดเต็ม

import seaborn as sb
import pandas as pd
import matplotlib.pyplot as plt

dataFrame = pd.read_csv("C:\\Users\\amit_\\Desktop\\Cricketers2.csv")
sb.set_theme(style="whitegrid")

# จัดกลุ่ม swarms ตาม Role (x) และ Age (y)
sb.swarmplot(x=dataFrame["Role"], y=dataFrame["Age"])
plt.show()

ผลลัพธ์

ได้ Swarm Plot แสดงการกระจายอายุของนักคริกเก็ตแยกตามบทบาท จุดไม่ทับซ้อนกัน