Machine Learning Academy · บทเรียน

การแสดงภาพข้อมูลด้วย Matplotlib และ Seaborn

ผู้เรียนจะสร้างฮิสโตแกรม แผนภาพกระจาย และแผนที่ความร้อนของสหสัมพันธ์ เพื่อสำรวจการกระจายและความสัมพันธ์ของข้อมูลก่อนสร้างแบบจำลอง

บทเรียน 4 จาก 413 ขั้นตอน

การแสดงภาพข้อมูลด้วย Matplotlib และ Seaborn เป็นบทเรียน Machine Learning Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Machine Learning Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Machine Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Visualisation Matters in ML

Charts aren't just for reports — they're a diagnostic tool at every step. Matplotlib gives full control; Seaborn makes beautiful stats plots with less code.

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

# Set seaborn theme for all plots
sns.set_theme(style='whitegrid', palette='muted')

# Load a built-in dataset
df = sns.load_dataset('tips')
print(df.head())

Histograms: Understanding Distributions

A histogram bins a numeric column to show its shape — normal, skewed, or with outliers. Spotting skew tells you when a log transform might help your model.

import matplotlib.pyplot as plt
import seaborn as sns

df = sns.load_dataset('tips')

fig, axes = plt.subplots(1, 2, figsize=(12, 4))

# Raw distribution (right-skewed)
axes[0].hist(df['total_bill'], bins=30, edgecolor='white')
axes[0].set_title('Total Bill Distribution (Raw)')

# After log transform
import numpy as np
axes[1].hist(np.log(df['total_bill']), bins=30, edgecolor='white')
axes[1].set_title('Total Bill Distribution (Log Transformed)')

plt.tight_layout()
plt.show()

Scatter Plots: Feature Relationships

A scatter plot shows how two numbers relate — linear, curved, or full of outliers. Add color with hue to squeeze a third variable into the same view.

import matplotlib.pyplot as plt
import seaborn as sns

df = sns.load_dataset('tips')

# Scatter plot with hue encoding
sns.scatterplot(
    data=df,
    x='total_bill',
    y='tip',
    hue='time',      # encode meal time as colour
    size='size',     # encode party size as dot size
    alpha=0.7
)
plt.title('Tip vs Total Bill (coloured by Meal Time)')
plt.show()

Box Plots: Comparing Groups

A box plot shows the median, spread, and outliers across groups. If a feature's box looks very different per category, that feature is probably worth keeping.

import matplotlib.pyplot as plt
import seaborn as sns

df = sns.load_dataset('tips')

fig, axes = plt.subplots(1, 2, figsize=(12, 5))

sns.boxplot(data=df, x='day', y='total_bill', ax=axes[0])
axes[0].set_title('Bill by Day of Week')

sns.boxplot(data=df, x='smoker', y='tip', hue='sex', ax=axes[1])
axes[1].set_title('Tip by Smoking Status and Sex')

plt.tight_layout()
plt.show()

Correlation Heatmap: Finding Related Features

A correlation heatmap colors how strongly every pair of columns moves together. It reveals good predictors of your target — and redundant features to drop.

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

df = pd.read_csv('titanic.csv')

# Compute correlation matrix
corr = df[['Survived', 'Pclass', 'Age', 'SibSp', 'Parch', 'Fare']].corr()

# Plot heatmap
plt.figure(figsize=(8, 6))
sns.heatmap(
    corr,
    annot=True,     # show correlation values
    fmt='.2f',      # 2 decimal places
    cmap='RdYlGn',  # red-yellow-green colour scale
    vmin=-1, vmax=1
)
plt.title('Feature Correlation Matrix')
plt.show()

Pair Plots: Exploring All Feature Pairs

A pair plot shows scatter plots for every feature pair at once — a fast first look at a new dataset. Color by class to see which features separate the groups.

import seaborn as sns
import matplotlib.pyplot as plt

# Use the iris dataset (classic ML benchmark)
df = sns.load_dataset('iris')

# Pair plot coloured by species
sns.pairplot(
    df,
    hue='species',
    diag_kind='kde',    # KDE on diagonal
    plot_kws={'alpha': 0.6}
)
plt.suptitle('Iris Dataset Pair Plot', y=1.02)
plt.show()

Bar Charts and Count Plots

A count plot shows how often each category appears — the quickest way to check class balance before training a classifier. Add hue to compare two categories.

import seaborn as sns
import matplotlib.pyplot as plt

df = sns.load_dataset('titanic')

fig, axes = plt.subplots(1, 2, figsize=(12, 5))

# Class distribution (check balance)
sns.countplot(data=df, x='survived', ax=axes[0])
axes[0].set_title('Survival Count')

# Class by passenger class and sex
sns.countplot(data=df, x='class', hue='sex', ax=axes[1])
axes[1].set_title('Class Distribution by Sex')

plt.tight_layout()
plt.show()

Plotting Learning Curves

A learning curve plots training vs validation score as data grows. Both low means underfitting; a big gap means overfitting; both high and close means a good fit.

import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection import learning_curve
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_breast_cancer

X, y = load_breast_cancer(return_X_y=True)
train_sizes, train_scores, val_scores = learning_curve(
    DecisionTreeClassifier(max_depth=5), X, y, cv=5
)

plt.plot(train_sizes, train_scores.mean(axis=1), label='Training Score')
plt.plot(train_sizes, val_scores.mean(axis=1), label='Validation Score')
plt.xlabel('Training Set Size')
plt.ylabel('Accuracy')
plt.legend()
plt.title('Learning Curve')
plt.show()

Visualising Model Predictions

After training, plot the predictions. A confusion matrix heatmap shows where a classifier confuses labels — far more telling than a single accuracy number.

import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split

X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)

model = DecisionTreeClassifier(max_depth=3)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)

cm = confusion_matrix(y_test, y_pred)
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.title('Confusion Matrix')
plt.show()

Saving and Customising Plots

Good plots need polish: titles, axis labels, and readable fonts. Save them with savefig at dpi=150+ for reports, and pick a colorblind-friendly palette.

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

# Create figure and axes explicitly
fig, ax = plt.subplots(figsize=(8, 5))

x = np.linspace(0, 10, 100)
ax.plot(x, np.sin(x), label='sin(x)', linewidth=2)
ax.plot(x, np.cos(x), label='cos(x)', linewidth=2, linestyle='--')

# Customise
ax.set_xlabel('x', fontsize=13)
ax.set_ylabel('y', fontsize=13)
ax.set_title('Sine and Cosine', fontsize=15, fontweight='bold')
ax.legend(fontsize=12)
ax.grid(True, alpha=0.3)

# Save
fig.savefig('plot.png', dpi=150, bbox_inches='tight')
plt.show()

Distribution Plots with Seaborn

A violin plot blends a box plot with a density curve, showing the full shape of a distribution. Seaborn's displot and kdeplot are great for smooth comparisons.

import seaborn as sns
import matplotlib.pyplot as plt

df = sns.load_dataset('tips')

fig, axes = plt.subplots(1, 2, figsize=(12, 5))

# Violin plot
sns.violinplot(data=df, x='day', y='total_bill', hue='sex',
               split=True, inner='quart', ax=axes[0])
axes[0].set_title('Bill Distribution by Day (Violin)')

# KDE distribution comparison
sns.kdeplot(data=df, x='tip', hue='time', fill=True, alpha=0.4, ax=axes[1])
axes[1].set_title('Tip Distribution by Meal Time (KDE)')

plt.tight_layout()
plt.show()

Quick Check

Test your understanding of Machine Learning with Python concepts from this lesson.

Lesson Recap

You learned to see your data: histograms and scatter plots reveal shape, heatmaps find predictors, and learning curves diagnose fit. Next: your first model! 🚀

เริ่มต้นได้ฟรี

เรียนรู้ Python ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
30
บทเรียน
120

คำถามที่พบบ่อย

บทเรียน “การแสดงภาพข้อมูลด้วย Matplotlib และ Seaborn” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การแสดงภาพข้อมูลด้วย Matplotlib และ Seaborn” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Machine Learning Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Machine Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การแสดงภาพข้อมูลด้วย Matplotlib และ Seaborn”

ผู้เรียนจะสร้างฮิสโตแกรม แผนภาพกระจาย และแผนที่ความร้อนของสหสัมพันธ์ เพื่อสำรวจการกระจายและความสัมพันธ์ของข้อมูลก่อนสร้างแบบจำลอง คุณปฏิบัติ Machine Learning Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Machine Learning Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Machine Learning Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การแสดงภาพข้อมูลด้วย Matplotlib และ Seaborn” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Machine Learning Academy นี้ได้ไหม

ได้ บทเรียน Machine Learning Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การติดตั้ง Anaconda และ Jupyter Notebook
  2. พื้นฐาน NumPy: อาร์เรย์และการดำเนินการทางคณิตศาสตร์
  3. Pandas สำหรับการจัดการข้อมูล
  4. การแสดงภาพข้อมูลด้วย Matplotlib และ Seaborn
← กลับไปที่ Machine Learning Academy