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

[업무 자동화] 다수의 점수를 한꺼번에 각각의 그래프로 만들기 (파이썬 코드, 학생 점수로 그래프 만드는 프로그램, To Create a Simple Graph with Students' Scores in Python)

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

>> 추가 수정 사항 : 다수인 처리, 100점이 없는 경우에 100점 횟수 노출하지 않기, 데이터가 없는 경우에 그 부분도 노출하지 않기, 엑셀파일의 데이터 바로 처리하기, 마커색깔 핫핑크로

 

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['Unit'] - 1) * 4 + df['Lesson']

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

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

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

    # 선 그래프 그리기
    plt.figure(figsize=(8, 5))

    # 그래프에서 점수 표시 (수정된 점수로 그래프 그리기)
    valid_data = student_data.dropna(subset=['Score'])  # 점수가 있는 데이터만 사용
    plt.plot(valid_data['Global_Lesson'], valid_data['Score'], marker='o', label="Score Trend")

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

    # 그래프 제목에 아이의 이름 포함
    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, 100)  # 0부터 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()

 


>> 결과물

>> 엑셀 데이터 저장 형식

 

>> 개선의 여지

1. 50점 이하 그래프 부분을 축소하고 물결 표시하는 게 구현 안됨.

2. 그래프가 딱딱하다.

3. 저장을 일일이, 구역을 정하여, 포맷을 지정해야 한다. - 모든 게 노출되고, 바로 jpeg로 저장됐으면 좋겠다.

 

1은 여러번 시도했는데 잘 안되고, 2, 3은 가능하겠지만 오늘은 여기까지😐

728x90
반응형