본문 바로가기
코딩 수업/파이썬 (업무자동화)

[업무 자동화] 기능추가 - 한 달의 데이터만 넣으면 돌아가는, 새로운 교재 표시선 추가 (파이썬 코드, 학생 점수로 그래프 만드는 프로그램, To Create a Simple Graph with Students' Scores in Python)

by Jade S. 2025. 1. 19.
728x90
반응형

>> 재재수정

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib

# 한글 폰트 설정
matplotlib.rcParams['font.family'] = 'Malgun Gothic'  # Windows에서 사용할 수 있는 한글 폰트
matplotlib.rcParams['axes.unicode_minus'] = False  # 마이너스 기호가 깨지지 않도록 설정

# 엑셀 파일 경로
file_path = "D:\\202501QuizScores.xlsx"

# 엑셀 파일 읽기
df = pd.read_excel(file_path)

# Lesson을 연속된 번호로 변경 (학생별로 순차적으로 증가)
df['Global_Lesson'] = df.groupby('Name').cumcount() + 1

# 각 학생 처리
students = df['Name'].unique()

for student in students:
    student_data = df[df['Name'] == student]

    # 점수가 없는 부분 제거
    student_data = student_data.dropna(subset=['Score'])

    # 새로운 Unit 1과 Lesson 1이 동시에 시작되는 지점 찾기
    new_unit1_starts = student_data[(student_data['Unit'] == 1) & (student_data['Lesson'] == 1)]['Global_Lesson']

    # 선 그래프 그리기
    plt.figure(figsize=(8, 5))
    plt.plot(student_data['Global_Lesson'], student_data['Score'], marker='o', label="Score Trend")

    # Lesson 4 강조: 핑크색 마커와 더 큰 마커
    lesson4 = student_data[student_data['Lesson'] == 4]
    plt.scatter(lesson4['Global_Lesson'], lesson4['Score'], color='hotpink', s=100, label='Unit Test (Lesson 4)', zorder=5)

    # 새로운 Unit 1 시작 지점에 세로선 추가
    for start in new_unit1_starts:
        plt.axvline(x=start, color='green', linestyle='--', linewidth=1.5, label='New Book Start')

    # 그래프 제목에 학생의 이름 포함
    plt.title(f"Score Trend for {student} Across Units", fontsize=16)

    # 그래프 꾸미기
    plt.xlabel("Lesson", fontsize=12)
    plt.ylabel("Score", fontsize=12)
    plt.xticks(student_data['Global_Lesson'], labels=[f"U{u}L{l}" for u, l in zip(student_data['Unit'], student_data['Lesson'])])
    plt.legend()
    plt.grid()

    # Y축 범위 설정
    plt.ylim(0, 110)

    # 우측 여백을 만들어 평균 점수와 100점 횟수 표시
    plt.subplots_adjust(right=0.85, bottom=0.1)

    # 평균 점수 계산
    average_score = student_data['Score'].mean()

    # 100점 횟수 계산
    perfect_scores = (student_data['Score'] == 100).sum()

    # 평균 점수 추가
    plt.text(1.05, 0.12, f"평균 점수: {average_score:.2f}", fontsize=10, verticalalignment='bottom', horizontalalignment='left', transform=plt.gca().transAxes)

    # 100점이 있을 경우에만 "100점 횟수" 추가
    if perfect_scores > 0:
        plt.text(1.05, 0.07, f"100점: {perfect_scores}번", fontsize=10, verticalalignment='bottom', horizontalalignment='left', transform=plt.gca().transAxes)

    # 그래프 출력
    plt.show()

 

>> 결과 값

728x90
반응형