문제 1 : 기초 선 그래프의 해석
다음은 어떤 웹사이트의 7일간 방문자 수 데이터입니다.
# 데이터 준비
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
visitors = [120, 135, 150, 140, 160, 180, 170]
1-1 하루 방문자 수 추이를 선 그래프로 그리되, 마커와 색상을 명시하라.
plt.figure(figsize=(6, 4))
plt.plot(days, visitors, marker='o', color='green')
plt.title('Daily Website Visitors')
plt.xlabel('Day')
plt.ylabel('Visitors')
plt.grid(True)
plt.show()
1-2 일자에 따른 증감폭(전일 대비 변화량)을 막대 그래프로 그려라.
diff = np.diff(visitors)
days_diff = days[1:]
bars = plt.bar(days_diff, diff, color='orange')
# 각 바 위에 값 레이블 표시
for bar in bars:
y = bar.get_height()
# 양수는 위에, 음수는 아래에 레이블
va = 'bottom' if y >= 0 else 'top'
offset = 1 if y >= 0 else -1
plt.text(
bar.get_x() + bar.get_width() / 2, # x 위치: 바 중앙
y , # y 위치: 값 끝
f"{int(y)}", # 텍스트
ha='center', va=va,
color='green'
)
plt.title('Daily Change in Visitors')
plt.show()
1-3 최대 방문자 수와 최소 방문자 수에 점을 찍고 레이블을 표시하라.
max_idx = np.argmax(visitors)
min_idx = np.argmin(visitors)
plt.plot(days, visitors, marker='o')
plt.annotate(
"Max",
xy=(days[max_idx], visitors[max_idx]),
xytext=(0, 20),
textcoords="offset points")
plt.annotate(
"Min",
xy=(days[min_idx], visitors[min_idx]),
xytext=(0, -40),
textcoords="offset points")
plt.title('Annotated Peak and Low')
plt.show()
문제 2 : 여러 개의 선 그래프 비교
다음은 두 지역의 월별 강수량 데이터입니다.
# 데이터 준비
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
region1 = [30, 40, 50, 80, 100, 150, 200, 180, 120, 90, 60, 40]
region2 = [20, 30, 45, 70, 110, 160, 190, 170, 110, 80, 55, 35]
2-1 두 지역의 강수량을 한 그래프에 선형으로 표시하고 범례를 붙이세요.
plt.plot(months, region1, label='Region 1')
plt.plot(months, region2, label='Region 2')
plt.title('Monthly Rainfall')
plt.legend()
plt.show()
2-2 한 그래프 안에서 두 지역 간 차이를 시각적으로 강조하세요.
# 차이 계산
diff = np.array(region1) - np.array(region2)
# 플롯
plt.plot(months, diff, color='purple')
plt.axhline(0, color='gray', linestyle='--')
# 레이블과 타이틀
plt.title("Rainfall Difference (Region1 - Region2)")
plt.show()
2-3 각 지역의 연간 총 강수량을 비교하는 막대 그래프를 그리세요.
total1 = sum(region1)
total2 = sum(region2)
plt.bar(['Region 1', 'Region 2'], [total1, total2], color=['blue', 'green'])
plt.title('Total Yearly Rainfall')
plt.show()
문제 3 : 히스토그램과 분포 분석
어떤 수학 시험 점수 데이터가 다음과 같이 주어집니다.
# 데이터 준비
scores = [55, 60, 62, 65, 67, 70, 72, 75, 78, 80, 82, 85, 87, 89, 90, 92, 95, 97, 99]
3-1 점수 분포를 히스토그램으로 10구간으로 나누어 그려라.
plt.hist(scores, bins=10, edgecolor='black', color='skyblue')
plt.title('Score Distribution')
plt.xlabel('Score')
plt.ylabel('Frequency')
plt.show()
3-2 평균 점수와 표준편차를 표시하라.
mean_score = np.mean(scores)
std_score = np.std(scores)
plt.hist(scores, bins=10, color='lightgray')
plt.axvline(mean_score, color='red', linestyle='-', label=f'Mean: {mean_score:.1f}')
plt.axvline(mean_score + std_score, color='blue', linestyle='--',
label=f'+1 Std: {mean_score + std_score:.1f}')
plt.title('Mean and Standard Deviation')
plt.legend()
plt.show()
3-3 1등급(90점 이상) 비율을 파이차트로 시각화하라.
high = sum(1 for s in scores if s >= 90)
low = len(scores) - high
plt.pie([high, low],
labels=['over 90 points', 'less than 90 point',],
autopct='%1.1f%%',
colors=['gold', 'lightgray']
)
plt.title('1st rate')
plt.show()
LIST