สรุปทุกอย่างเข้าด้วยกัน

การสร้างภาพข้อมูลด้วย Seaborn เบื้องต้น

Content Team

DataCamp

เริ่มต้นใช้งาน

นำเข้า Seaborn:

import seaborn as sns

นำเข้า Matplotlib:

import matplotlib.pyplot as plt

แสดงกราฟ:

plt.show()
การสร้างภาพข้อมูลด้วย Seaborn เบื้องต้น

Relational plots

  • แสดงความสัมพันธ์ระหว่างตัวแปรเชิงปริมาณสองตัว
  • ตัวอย่าง: scatter plot, line plot
sns.relplot(x="x_variable_name", 
            y="y_variable_name", 
            data=pandas_df, 
            kind="scatter")
การสร้างภาพข้อมูลด้วย Seaborn เบื้องต้น

Categorical plots

  • แสดงการกระจายของตัวแปรเชิงปริมาณภายในหมวดหมู่ที่กำหนดโดยตัวแปรเชิงหมวดหมู่
  • ตัวอย่าง: bar plot, count plot, box plot, point plot
sns.catplot(x="x_variable_name", 
            y="y_variable_name", 
            data=pandas_df, 
            kind="bar")
การสร้างภาพข้อมูลด้วย Seaborn เบื้องต้น

เพิ่มตัวแปรที่สาม (hue)

การกำหนด hue จะสร้างกลุ่มย่อยที่แสดงด้วยสีต่างกันบนกราฟเดียวกัน

Scatter plot ที่ใช้ hue

1 Waskom, M. L. (2021). seaborn: statistical data visualization. https://seaborn.pydata.org/
การสร้างภาพข้อมูลด้วย Seaborn เบื้องต้น

เพิ่มตัวแปรที่สาม (row/col)

การกำหนด row และ/หรือ col ใน relplot() หรือ catplot() จะสร้างกลุ่มย่อยที่แสดงบน subplot แยกกัน

Scatter plot ที่มี subplot

1 Waskom, M. L. (2021). seaborn: statistical data visualization. https://seaborn.pydata.org/
การสร้างภาพข้อมูลด้วย Seaborn เบื้องต้น

การปรับแต่ง

  • เปลี่ยนพื้นหลัง: sns.set_style()
  • เปลี่ยนสีขององค์ประกอบหลัก: sns.set_palette()
  • เปลี่ยนสเกล: sns.set_context()
การสร้างภาพข้อมูลด้วย Seaborn เบื้องต้น

การเพิ่มชื่อกราฟ

ประเภท Object ประเภท Plot วิธีเพิ่มชื่อกราฟ
FacetGrid relplot(), catplot() g.figure.suptitle()
AxesSubplot scatterplot(), countplot(), etc. g.set_title()
การสร้างภาพข้อมูลด้วย Seaborn เบื้องต้น

การปรับแต่งขั้นสุดท้าย

เพิ่มป้ายกำกับแกน x และ y:

g.set(xlabel="new x-axis label",
      ylabel="new y-axis label")

หมุนป้ายกำกับแกน x:

plt.xticks(rotation=90)
การสร้างภาพข้อมูลด้วย Seaborn เบื้องต้น

มาฝึกกันเถอะ!

การสร้างภาพข้อมูลด้วย Seaborn เบื้องต้น

Preparing Video For Download...