파이썬의 대표적인 시각화 라이브러리인 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()
'Python' 카테고리의 다른 글
| 네이버 뉴스 페이지에서 헤드라인 하나를 가져오는 예시입니다. (0) | 2026.01.07 |
|---|---|
| 실습: 웹사이트의 표를 내 컴퓨터로 가져오기 (0) | 2026.01.07 |
| 📚 실습 2: 신문사 홈피에서 뉴스 목록 만들기 (0) | 2026.01.07 |
| 실습: 뉴스 기사 본문 가져오기 (0) | 2026.01.07 |
| 수료증 만들기 (0) | 2026.01.05 |