선그래프, 막대그래프, 원그래프, 히스토그램, 산점도, 인포그래픽을 실생활 예제로 배우는 파이썬 수업자료
데이터를 표나 숫자만으로 보는 대신, 그래프와 도형으로 표현하여 더 빠르고 정확하게 이해하는 방법입니다.
| 그래프 종류 | 주로 보는 것 |
|---|---|
| 선그래프 | 시간에 따른 변화 |
| 막대그래프 | 항목 간 비교 |
| 원그래프 | 전체 중 비율 |
| 히스토그램 | 값의 분포 |
| 산점도 | 두 변수의 관계 |
| 인포그래픽 | 핵심 정보 전달 |
시간의 흐름에 따라 값이 어떻게 변하는지 볼 때 가장 적합합니다.
하루 시간대별 기온 변화
import matplotlib.pyplot as plt
import platform
if platform.system() == "Windows":
plt.rcParams["font.family"] = "Malgun Gothic"
elif platform.system() == "Darwin":
plt.rcParams["font.family"] = "AppleGothic"
else:
plt.rcParams["font.family"] = "NanumGothic"
plt.rcParams["axes.unicode_minus"] = False
times = ["06시", "09시", "12시", "15시", "18시", "21시"]
temps = [12, 15, 21, 24, 20, 16]
plt.figure(figsize=(8, 4))
plt.plot(times, temps, marker="o")
plt.title("하루 시간대별 기온 변화")
plt.xlabel("시간")
plt.ylabel("기온(℃)")
plt.grid(True)
plt.tight_layout()
plt.show()서로 다른 항목의 크기를 비교할 때 가장 직관적입니다.
과목별 평균 점수 비교
subjects = ["국어", "수학", "영어", "과학", "정보"]
scores = [84, 92, 88, 95, 90]
plt.figure(figsize=(8, 4))
plt.bar(subjects, scores)
plt.title("과목별 평균 점수")
plt.xlabel("과목")
plt.ylabel("평균 점수")
plt.ylim(0, 100)
plt.tight_layout()
plt.show()전체에서 각 부분이 차지하는 비율을 보여줄 때 적합합니다.
스마트폰 사용 목적 비율
labels = ["공부", "게임", "영상 시청", "메신저", "기타"]
sizes = [25, 20, 30, 15, 10]
plt.figure(figsize=(6, 6))
plt.pie(sizes, labels=labels, autopct="%1.1f%%", startangle=90)
plt.title("스마트폰 사용 목적 비율")
plt.tight_layout()
plt.show()값이 어느 구간에 많이 몰려 있는지, 즉 데이터의 분포를 볼 때 사용합니다.
학생들의 수면 시간 분포
sleep_hours = [5.5, 6, 6, 6.5, 7, 7, 7, 7.5, 8, 8, 8.5,
6.5, 7.2, 6.8, 7.1, 5.8, 6.3, 7.7, 8.1, 6.9]
plt.figure(figsize=(8, 4))
plt.hist(sleep_hours, bins=6, edgecolor="black")
plt.title("학생들의 수면 시간 분포")
plt.xlabel("수면 시간")
plt.ylabel("학생 수")
plt.tight_layout()
plt.show()두 변수 사이의 관계를 파악할 때 사용합니다.
공부 시간과 시험 점수 관계
study_hours = [1, 2, 2.5, 3, 4, 4.5, 5, 6, 6.5, 7, 8]
scores = [50, 55, 58, 65, 70, 72, 78, 83, 85, 90, 94]
plt.figure(figsize=(8, 4))
plt.scatter(study_hours, scores)
plt.title("공부 시간과 시험 점수의 관계")
plt.xlabel("공부 시간")
plt.ylabel("시험 점수")
plt.grid(True)
plt.tight_layout()
plt.show()복잡한 그래프 대신 핵심 수치를 강하게 전달하고 싶을 때 적합합니다.
한 학생의 하루 생활 데이터 요약
study_time = 5.5
sleep_time = 7.0
phone_time = 2.5
steps = 8420
fig = plt.figure(figsize=(10, 6))
ax = fig.add_axes([0, 0, 1, 1])
ax.axis("off")
plt.text(0.5, 0.9, "하루 생활 데이터 인포그래픽",
ha="center", fontsize=20, weight="bold")
plt.text(0.2, 0.65, "공부 시간", ha="center", fontsize=14)
plt.text(0.2, 0.55, f"{study_time}시간", ha="center", fontsize=24, weight="bold")
plt.text(0.5, 0.65, "수면 시간", ha="center", fontsize=14)
plt.text(0.5, 0.55, f"{sleep_time}시간", ha="center", fontsize=24, weight="bold")
plt.text(0.8, 0.65, "스마트폰 사용", ha="center", fontsize=14)
plt.text(0.8, 0.55, f"{phone_time}시간", ha="center", fontsize=24, weight="bold")
plt.text(0.5, 0.3, "걸음 수", ha="center", fontsize=14)
plt.text(0.5, 0.2, f"{steps}보", ha="center", fontsize=28, weight="bold")
plt.show()| 그래프 | 언제 쓰는가? | 실생활 예제 |
|---|---|---|
| 선그래프 | 시간에 따른 변화 | 하루 기온 변화 |
| 막대그래프 | 항목 비교 | 과목별 평균 점수 |
| 원그래프 | 전체 중 비율 | 스마트폰 사용 목적 |
| 히스토그램 | 데이터 분포 | 수면 시간 분포 |
| 산점도 | 두 변수의 관계 | 공부 시간과 시험 점수 |
| 인포그래픽 | 핵심 정보 요약 | 하루 생활 데이터 |