Seaborn: วิธีสร้างและตกแต่งกราฟใน Python พร้อมตัวอย่าง

Seaborn is a Python package for data visualisation.

Updated: 03 Nov 2026

.


🤔 Seaborn คืออะไร?

seaborn เป็น package ยอดนิยมสำหรับสร้างกราฟ (data visualisation) ใน Python และมีจุดเด่น 3 ข้อ

  1. ใช้งานง่าย ด้วยการเขียนโค้ดที่ตรงไปตรงมา
  2. กราฟมีหน้าตาสวยงาม เหมาะสำหรับตีพิมพ์
  3. ทำงานร่วมกับ pandas และ matplotlib ซึ่งเป็น package ยอดนิยมสำหรับทำงานกับข้อมูล ได้อย่างลงตัว

💻 การเริ่มใช้งาน seaborn

เราเริ่มใช้งาน seaborn ได้ใน 2 ขั้นตอน:

  1. ติดตั้ง package
  2. โหลด package

ขั้นที่ 1. ติดตั้ง package:

ติดตั้ง seaborn รวมทั้ง pandas และ matplotlib ที่จะใช้งานร่วมกัน (ทำแค่ครั้งแรกครั้งเดียว)

! pip install seaborn pandas matplotlib

ขั้นที่ 2. โหลด package:

โหลด seaborn และ package ที่เกี่ยวข้อง (ทำทุกครั้งที่เริ่ม session ใหม่)

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

🧑‍💻 วิธีใช้งาน seaborn

seaborn มีวิธีสร้างกราฟดังนี้:

# Create a plot
sns.plot(data, x, y, customisation)
# Show the plot
plt.show()
  • sns.plot กำหนดประเภทกราฟ
  • data กำหนดชุดข้อมูล
  • x กำหนดข้อมูลแกน x
  • y กำหนดข้อมูลแกน y
  • customisation ตกแต่งกราฟ
  • plt.show() แสดงกราฟบนหน้าจอ

📉 การสร้างกราฟ 5 ประเภท

มาดูวิธีสร้างกราฟ 5 ประเภทใน seaborn กัน:

  1. Histogram
  2. Box plot
  3. Scatter plot
  4. Line plot
  5. Bar plot

.

🍔 ชุดข้อมูลตัวอย่าง

สำหรับชุดข้อมูลตัวอย่าง เราจะใช้ tips ซึ่งมีข้อมูลเกี่ยวกับทิปในร้านอาหารกัน

รายละเอียดชุดข้อมูล:

คอลัมน์คำอธิบาย
total_billราคารวมค่าอาหาร
tipทิปที่ได้รับ
sexเพศของลูกค้า
smokerการสูบบุหรี่ของลูกค้า (สูบ หรือ ไม่สูบ)
dayวันของสัปดาห์
timeช่วงเวลาของวัน (lunch หรือ dinner)
sizeจำนวนแขกในโต๊ะเดียวกัน

วิธีโหลดข้อมูล:

tips = sns.load_dataset("tips")
Tips is a built-in dataset in seaborn.

.

📊 กราฟ 1. Histogram

Histogram เป็นกราฟแสดงการกระจายตัว (distribution) ของข้อมูล

ตัวอย่าง:

ดูการกระจายตัวของทิปที่พนักงานได้รับ

# Create a histogram
sns.histplot(
data = tips,
x = "tip"
)
# Show the plot
plt.show()

ผลลัพธ์:

Use sns.histplot() to create a histogram in seaborn.
Histogram

.

📊 กราฟ 2. Box Plot

Box plot แสดงการกระจายตัวของข้อมูล (เหมือนกับ histogram) และใช้เปรียบเทียบการกระจายตัวของตัวแปรหนึ่งระหว่างกลุ่มได้

ตัวอย่าง:

ดูการกระจายตัวของทิปในแต่ละมื้ออาหาร

# Create a box plot
sns.boxplot(
data = tips,
x = "time",
y = "tip"
)
# Show the plot
plt.show()

ผลลัพธ์:

Use sns.boxplot() to create a box plot in seaborn.
Box plot

.

📊 กราฟ 3. Scatter Plot

Scatter plot ใช้แสดงความสัมพันธ์ระหว่าง 2 ตัวแปร

ตัวอย่าง:

ความสัมพันธ์ระหว่างค่าอาหารและทิป

# Create a scatter plot
sns.scatterplot(
data = tips,
x = "total_bill",
y = "tip"
)
# Show the plot
plt.show()

ผลลัพธ์:

Use sns.scatterplot() to create a scatter plot in seaborn.
Scatter plot

.

📊 กราฟ 4. Line Plot

Line plot ใช้แสดงความสัมพันธ์ระหว่างตัวแปร 2 ตัว หรือแสดงการเปลี่ยนแปลงของข้อมูลตามช่วงเวลา

ตัวอย่าง:

สำรวจทิปตามจำนวนแขก

# Create a line plot
sns.lineplot(
data = tips,
x = "size",
y = "tip"
)
# Show the plot
plt.show()

ผลลัพธ์:

Use sns.lineplot() to create a line plot in seaborn.
Line plot

.

📊 กราฟ 5. Bar Plot

Bar plot ใช้เปรียบเทียบค่าของตัวแปรระหว่างกลุ่ม

ตัวอย่าง:

สำรวจทิปในแต่ละวันของสัปดาห์

# Create a bar plot
sns.barplot(
data = tips,
x = "day",
y = "tip"
)
# Show the plot
plt.show()

ผลลัพธ์:

Use sns.barplot() to create a bar plot in seaborn.
Bar plot

3️⃣ การเพิ่มตัวแปรที่ 3

เราสามารถเพิ่มตัวแปรที่ 3 (นอกเหนือจากแกน x, y) ได้โดยใช้สี (hue) ดังนี้

ตัวอย่าง:

เพิ่มประเภทการสูบบุหรี่ (ตัวแปรที่ 3) เข้าไปในกราฟความสัมพันธ์ระหว่างทิป (y) และค่าอาหาร (x)

# Create a scatter plot
sns.scatterplot(
data = tips,
x = "total_bill",
y = "tip",
hue = "smoker" # third variable
)
# Show the plot
plt.show()

ผลลัพธ์:

Use hue argument to add a third variable into a plot in seaborn.
การเพิ่มตัวแปรที่ 3 ด้วย hue

🎨 การตกแต่งกราฟ

เรามาดู 3 วิธีในการตกแต่งกราฟกัน:

  1. การเปลี่ยนสีข้อมูล
  2. การเปลี่ยนสไตล์กราฟ
  3. การเพิ่มข้อความ

.

🖌️ วิธีที่ 1. การเปลี่ยนสีข้อมูล

เปลี่ยนสีข้อมูล (hue) ได้ด้วย 2 วิธีย่อย:

  1. ใช้ palette: กำหนดสีเอง
  2. ใช้ sns.set_palette(): ใช้เซตสีของ seaborn

👉 ตัวอย่างการใช้ palette:

เปลี่ยนสีประเภทลูกค้าให้เป็นสีแดงและดำ

# Specify the colours
colours = {
"Yes": "red",
"No": "black"
}
# Create a scatter plot
sns.scatterplot(
data = tips,
x = "total_bill",
y = "tip",
hue = "smoker",
palette = colours # change colours
)
# Show the plot
plt.show()

ผลลัพธ์:

Use palette argument to adjust the colours of the data points in seaborn plots.
เปลี่ยนสีกราฟด้วย palette

👉 ตัวอย่างการใช้ sns.set_palette():

ใช้ชุดสีที่เหมาะกับผู้ชมที่ตาบอดสี

# Set the palette
sns.set_palette("colorblind")
# Create a scatter plot
sns.scatterplot(
data = tips,
x = "total_bill",
y = "tip",
hue = "smoker"
)
# Show the plot
plt.show()

ผลลัพธ์:

Use sns.set_palette() to adjust the colours of the data points in seaborn plots.
เปลี่ยนสีกราฟด้วย sns.set_palette()

ตัวอย่างเซตสีที่เลือกใช้ได้:

Argumentค่าสี
"deep"ค่าตั้งต้นของ seaborn
"muted""deep" เวอร์ชันสีอ่อน
"pastel"สีพาสเทล
"dark"สีเข้ม
"colorblind"สีสำหรับคนตาบอดสี

.

🖌️ วิธีที่ 2. การเปลี่ยนสไตล์กราฟ

เราเปลี่ยนสไตล์กราฟ (ธีม) ด้วย sns.set_style() ได้ดังนี้:

Argumentสีพื้นหลังสีเส้นกราฟ
"white"ขาว ⚪ขาว ⚪
"dark"ดำ ⚫ดำ ⚫
"whitegrid"ขาว ⚪ดำ ⚫
"darkgrid"ดำ ⚫ขาว ⚪
"ticks"ขาว ⚪ไม่มี ✖️

ตัวอย่าง:

เปลี่ยนสไตล์กราฟให้มีฉากหลังสีเทา

# Set the style
sns.set_style("darkgrid")
# Create a scatter plot
sns.scatterplot(
data = tips,
x = "total_bill",
y = "tip",
hue = "smoker"
)
# Show the plot
plt.show()

ผลลัพธ์:

Use sns.set_style() to adjust the plot theme or style in seaborn.
เปลี่ยนสไตล์กราฟด้วย sns.set_style()

.

🖌️ วิธีที่ 3. การเพิ่มข้อความ

เราเพิ่มข้อความลงบนกราฟได้ด้วยคำสั่งจาก matplotlib.pyplot ดังนี้

ตัวอย่าง:

# Create a scatter plot
sns.scatterplot(
data = tips,
x = "total_bill",
y = "tip",
hue = "smoker"
)
# Add a title
plt.title("Total Bill vs Tip", fontsize = 16)
# Add labels
plt.xlabel("Total Bill ($)", fontsize = 12)
plt.ylabel("Tip ($)", fontsize = 12)
# Show the plot
plt.show()

ผลลัพธ์:

Use plt.title(), plt.xlabel(), plt.ylabel() to add texts to seaborn plots.
เพิ่มข้อความด้วยคำสั่งจาก matplotlib.pyplot

💪 สรุปเนื้อหา

ในบทความนี้ เราเรียนรู้วิธีการสร้างกราฟง่าย ๆ ใน seaborn กัน

การสร้างกราฟ 5 ประเภท:

กราฟคำสั่ง
Histogramsns.histplot()
Box plotsns.boxplot()
Scatter plotsns.scatterplot()
Line plotsns.lineplot()
Bar plotsns.barplot()

การเพิ่มตัวแปรที่ 3:

วิธีคำสั่ง
เพิ่มผ่านสีhue

การตกแต่งกราฟ:

ปรับแต่งคำสั่ง
สีข้อมูลpalette หรือ sns.set_palette()
สไตล์กราฟsns.set_style()
ข้อความplt.title() และ plt.xlabel() และ plt.ylabel()
Seaborn is a Python package for data visualisation.

🫵 หลังจบบทความนี้

ลองมาใช้ seaborn เพื่อสร้างกราฟกันนะครับ


📄 อ้างอิง

Comments

One response to “Seaborn: วิธีสร้างและตกแต่งกราฟใน Python พร้อมตัวอย่าง”

  1. […] packages ยอดนิยมอย่าง Matplotlib และ Seaborn อยู่ 2 […]

Leave a Reply