본문 바로가기
Python

Matplotlib

by 헬로제이콥 2026. 1. 7.

파이썬의 대표적인 시각화 라이브러리인 Matplotlib을 활용해 가장 자주 쓰이는 3가지 그래프(선, 막대, 산점도) 예제


1. 선 그래프 (Line Plot)

데이터의 흐름이나 시간에 따른 변화를 보여줄 때 가장 적합합니다.

 

import matplotlib.pyplot as plt

# 데이터 준비
days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
temperature = [22, 24, 23, 25, 28, 26, 24]

# 그래프 생성
plt.plot(days, temperature, marker='o', color='skyblue', linestyle='--')

# 제목 및 라벨 추가
plt.title('Weekly Temperature')
plt.xlabel('Day')
plt.ylabel('Temperature (°C)')

# 그래프 표시
plt.show()

 

 

2. 막대 그래프 (Bar Chart)

항목 간의 수치를 비교할 때 효과적입니다.

import matplotlib.pyplot as plt

# 데이터 준비
fruits = ['Apple', 'Banana', 'Cherry', 'Orange']
sales = [15, 30, 10, 25]

# 그래프 생성
plt.bar(fruits, sales, color=['red', 'yellow', 'darkred', 'orange'])

# 제목 및 라벨 추가
plt.title('Fruit Sales')
plt.xlabel('Fruit')
plt.ylabel('Quantity Sold')

# 그래프 표시
plt.show()

3. 산점도 (Scatter Plot)

두 변수 간의 상관관계를 파악할 때 유용합니다.

Python
 
import matplotlib.pyplot as plt

# 데이터 준비 (공부 시간 vs 시험 점수)
study_hours = [1, 2, 3, 4, 5, 6, 7, 8, 9]
exam_scores = [50, 55, 65, 70, 75, 85, 88, 92, 95]

# 그래프 생성
plt.scatter(study_hours, exam_scores, color='green', s=100) # s는 점의 크기

# 제목 및 라벨 추가
plt.title('Study Hours vs Exam Score')
plt.xlabel('Hours Studied')
plt.ylabel('Score')

# 그리드(격자) 추가
plt.grid(True)

# 그래프 표시
plt.show()