플로팅 함수 정의
plot_student_results라는 함수를 정의합니다. 이 함수는 Student 튜플, 테스트별 점수 딕셔너리, 그리고 코호트 크기를 입력으로 받습니다. 이 함수는 학생의 학년 및 성별 코호트를 기준으로 각 테스트의 백분위 순위를 나타내는 수평 막대 차트를 생성합니다.
def plot_student_results(student, scores_by_test, cohort_size):
fig, ax1 = plt.subplots(figsize=(9, 7), layout='constrained')
fig.canvas.manager.set_window_title('Fitness Chart')
ax1.set_title(student.name)
ax1.set_xlabel(
'Percentile Ranking Across {grade} Grade {gender}s\n'
'Cohort Size: {cohort_size}'.format(
grade=to_ordinal(student.grade),
gender=student.gender.title(),
cohort_size=cohort_size))
test_names = list(scores_by_test.keys())
percentiles = [score.percentile for score in scores_by_test.values()]
rects = ax1.barh(test_names, percentiles, align='center', height=0.5)
large_percentiles = [to_ordinal(p) if p > 40 else '' for p in percentiles]
small_percentiles = [to_ordinal(p) if p <= 40 else '' for p in percentiles]
ax1.bar_label(rects, small_percentiles,
padding=5, color='black', fontweight='bold')
ax1.bar_label(rects, large_percentiles,
padding=-32, color='white', fontweight='bold')
ax1.set_xlim([0, 100])
ax1.set_xticks([0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
ax1.xaxis.grid(True, linestyle='--', which='major',
color='grey', alpha=.25)
ax1.axvline(50, color='grey', alpha=0.25)
ax2 = ax1.twinx()
ax2.set_ylim(ax1.get_ylim())
ax2.set_yticks(
np.arange(len(scores_by_test)),
labels=[format_score(score) for score in scores_by_test.values()])
ax2.set_ylabel('Test Scores')