Altair: สอนวิธีสร้างกราฟ Data Visualisation ใน Python

Data visualisation (data viz) เป็นการแสดงข้อมูลผ่านภาพกราฟิก เพื่อช่วยในการนำเสนอและทำความเข้าใจข้อมูล

Altair เป็น package ใน Python สำหรับสร้างกราฟข้อมูล ซึ่งมีจุดเด่นกว่า packages ยอดนิยมอย่าง Matplotlib และ Seaborn อยู่ 2 ข้อ:

  1. Declarative syntax: ใช้งานง่ายด้วยการเขียนที่เน้นสิ่งที่ต้องแสดงผล (what) แทนการกำหนดว่าจะแสดงอย่างไร (how)
  2. Interactive plotting: สามารถสร้างกราฟแบบ interactive ได้

ในบทความนี้ ผมจะพาทุกคนไปดูวิธีใช้ Altair เพื่อสร้างกราฟใน Python กัน

บทความนี้จะแบ่งเป็น 3 ส่วน:

  1. Dataset: แนะนำตัวอย่างข้อมูลที่จะใช้
  2. Basic plotting: การสร้างกราฟพื้นฐานใน Altair
  3. Customisation: การปรับแต่งกราฟใน Altair

ถ้าพร้อมแล้ว ไปเริ่มกันเลย



📦 Dataset ตัวอย่าง

ในบทความนี้ เราจะใช้ cars dataset จาก vega_datasets ซึ่งมีข้อมูลรถ เช่น:

  • ชื่อรุ่น
  • ระดับการกินน้ำมัน
  • แรงม้า
  • จำนวนลูกสูบ

เราสามารถโหลด dataset ได้แบบนี้:

Python
# Import the package
from vega_datasets import data
# Load the dataset
cars = data.cars()
# Preview the dataset
cars.head()

ผลลัพธ์:


📊 พื้นฐานการสร้างกราฟใน Altair

การสร้างกราฟใน Altair ประกอบด้วย 3 ส่วน:

ComponentFor
.Chart()กำหนด dataset
.mark_*()กำหนดประเภทกราฟ (เช่น กราฟแท่ง)
.encode()กำหนดการแสดงข้อมูล (เช่น แกน x-y, สี, รูปทรง)

ทั้ง 3 ส่วนทำงานรวมกันแบบนี้:

Python
# Import the package
import altair as alt
# Create a scatter plot
(
alt.Chart(cars) # Set dataset
.mark_point() # Set plot type
.encode(
x="Miles_per_Gallon",
y="Horsepower"
) # Map data
)

ผลลัพธ์:

Note: .mark_*() เรากำหนดประเภทกราฟได้ เช่น:

MarkPlot
.mark_boxplot()Box plot
.mark_point()Scatter plot
.mark_line()Line plot
.mark_bar()Bar plot

🎨 วิธีปรับแต่งกราฟใน Altair

เรามาดูการปรับแต่ง 3 องค์ประกอบในกราฟกัน:

  1. Title and labels: กำหนดชื่อกราฟและแกนข้อมูล
  2. Data points: ปรับแต่งการแสดงข้อมูล (เช่น สี รูปทรง)
  3. Third variable: การเพิ่มตัวแปรที่ 3

.

🚸 การเพิ่ม Title & Labels

เราสามารถเพิ่มชื่อกราฟและชื่อแกนได้ด้วย 3 คำสั่ง:

SyntaxFor
.properties(title)ชื่อกราฟ
alt.X()ชื่อแกน x
alt.Y()ชื่อแกน y

ตัวอย่าง:

Python
# Customise the title and axis labels
(
alt.Chart(cars)
.mark_point()
# Add axis labels
.encode(
x=alt.X("Miles_per_Gallon", title="MPG"),
y=alt.Y("Horsepower", title="Horsepower")
)
# Add the title
.properties(
title="Horsepower vs Fuel Consumption"
)
)

ผลลัพธ์:

.

🔢 การตกแต่ง Data Points

เราสามารถปรับการนำเสนอข้อมูล โดยกำหนด parametre ใน .mark_*() เช่น:

ParametreProperty
colorสี
shapeรูปทรง
sizeขนาด
opacityความโปร่งแสง

ตัวอย่าง:

Python
# Customise data points
(
alt.Chart(cars)
# Adjust colour, shape, opacity, size
.mark_point(
color="red",
shape="diamond",
size=50,
opacity=0.5
)
.encode(
x=alt.X("Miles_per_Gallon", title="MPG"),
y=alt.Y("Horsepower", title="Horsepower")
)
.properties(
title="Horsepower vs Fuel Consumption"
)
)

ผลลัพธ์:

.

🥉 การเพิ่ม Third Variable

เราสามารถเพิ่มตัวแปรอื่น ๆ ได้โดยกำหนด parametre ใน .encode() เช่น:

ParametreProperty
colorสี
shapeรูปทรง
sizeขนาด
opacityความโปร่งแสง

ตัวอย่าง:

Python
# Add a third variable
(
alt.Chart(cars)
.mark_point(color="red")
.encode(
x=alt.X("Miles_per_Gallon", title="MPG"),
y=alt.Y("Horsepower", title="Horsepower"),
# Add as colours
color="Origin"
)
.properties(
title="Horsepower vs Fuel Consumption"
)
)

ผลลัพธ์:


💪 บทสรุป

Altair เป็นเครื่องมือสร้างกราฟที่ใช้งานง่ายใน Python โดยมีส่วนประกอบ 3 ส่วนหลัก:

(
alt.Chart()
.mark_*()
.encode()
)

และสามารถปรับแต่งได้โดยการกำหนด parametre ใน:

  1. .mark_*(): ประเภทกราฟ
  2. .encode(): สำหรับการนำเสนอตัวแปร
  3. .properties(): สำหรับชื่อกราฟ

🫵 Your Turn

หลังอ่านบทความนี้กันแล้ว ลองมาใช้ Altair กันนะครับ


📃 References

Intro to Altair:

Using Altair:

Comparing Altair with other packages:

Fediverse Reactions

Comments

Leave a comment