Data visualisation (data viz) เป็นการแสดงข้อมูลผ่านภาพกราฟิก เพื่อช่วยในการนำเสนอและทำความเข้าใจข้อมูล
Altair เป็น package ใน Python สำหรับสร้างกราฟข้อมูล ซึ่งมีจุดเด่นกว่า packages ยอดนิยมอย่าง Matplotlib และ Seaborn อยู่ 2 ข้อ:
- Declarative syntax: ใช้งานง่ายด้วยการเขียนที่เน้นสิ่งที่ต้องแสดงผล (what) แทนการกำหนดว่าจะแสดงอย่างไร (how)
- Interactive plotting: สามารถสร้างกราฟแบบ interactive ได้
ในบทความนี้ ผมจะพาทุกคนไปดูวิธีใช้ Altair เพื่อสร้างกราฟใน Python กัน
บทความนี้จะแบ่งเป็น 3 ส่วน:
- Dataset: แนะนำตัวอย่างข้อมูลที่จะใช้
- Basic plotting: การสร้างกราฟพื้นฐานใน Altair
- Customisation: การปรับแต่งกราฟใน Altair
ถ้าพร้อมแล้ว ไปเริ่มกันเลย
📦 Dataset ตัวอย่าง
ในบทความนี้ เราจะใช้ cars dataset จาก vega_datasets ซึ่งมีข้อมูลรถ เช่น:
- ชื่อรุ่น
- ระดับการกินน้ำมัน
- แรงม้า
- จำนวนลูกสูบ
เราสามารถโหลด dataset ได้แบบนี้:
# Import the packagefrom vega_datasets import data# Load the datasetcars = data.cars()# Preview the datasetcars.head()
ผลลัพธ์:

📊 พื้นฐานการสร้างกราฟใน Altair
การสร้างกราฟใน Altair ประกอบด้วย 3 ส่วน:
| Component | For |
|---|---|
.Chart() | กำหนด dataset |
.mark_*() | กำหนดประเภทกราฟ (เช่น กราฟแท่ง) |
.encode() | กำหนดการแสดงข้อมูล (เช่น แกน x-y, สี, รูปทรง) |
ทั้ง 3 ส่วนทำงานรวมกันแบบนี้:
# Import the packageimport 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_*() เรากำหนดประเภทกราฟได้ เช่น:
| Mark | Plot |
|---|---|
.mark_boxplot() | Box plot |
.mark_point() | Scatter plot |
.mark_line() | Line plot |
.mark_bar() | Bar plot |
🎨 วิธีปรับแต่งกราฟใน Altair
เรามาดูการปรับแต่ง 3 องค์ประกอบในกราฟกัน:
- Title and labels: กำหนดชื่อกราฟและแกนข้อมูล
- Data points: ปรับแต่งการแสดงข้อมูล (เช่น สี รูปทรง)
- Third variable: การเพิ่มตัวแปรที่ 3
.
🚸 การเพิ่ม Title & Labels
เราสามารถเพิ่มชื่อกราฟและชื่อแกนได้ด้วย 3 คำสั่ง:
| Syntax | For |
|---|---|
.properties(title) | ชื่อกราฟ |
alt.X() | ชื่อแกน x |
alt.Y() | ชื่อแกน y |
ตัวอย่าง:
# 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_*() เช่น:
| Parametre | Property |
|---|---|
color | สี |
shape | รูปทรง |
size | ขนาด |
opacity | ความโปร่งแสง |
ตัวอย่าง:
# 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() เช่น:
| Parametre | Property |
|---|---|
color | สี |
shape | รูปทรง |
size | ขนาด |
opacity | ความโปร่งแสง |
ตัวอย่าง:
# 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 ใน:
.mark_*(): ประเภทกราฟ.encode(): สำหรับการนำเสนอตัวแปร.properties(): สำหรับชื่อกราฟ
🫵 Your Turn
หลังอ่านบทความนี้กันแล้ว ลองมาใช้ Altair กันนะครับ
📃 References
Intro to Altair:
Using Altair:
- Introduction to Altair in Python
- Altair in Python Tutorial: Data Visualizations
- Altair: Declarative Charts With Python
Comparing Altair with other packages:
