Analysis

[데이터마이닝] matplotlib - random 3 lap 과제

ialleejy 2024. 12. 5. 08:01

한글 폰트깨질 경우

import matplotlib 

# windows의 경우 matplotlib.rcParams['font.family'] = 'Malgun Gothic'

 

 

 

import numpy as np
import matplotlib.pyplot as plt
Score = np.random.randint(100, size=(10, 4))
korea_point=Score[:,0]
english_point=Score[:,1]
math_point=Score[:,2]
science_point=Score[:,3]
average_score = (korea_point + english_point + math_point + science_point)/4
average_score


"""
array([28.75, 49.75, 53.25, 72.75, 37.75, 75.  , 48.25, 39.75, 56.25,
       56.75])
"""

Score

"""
array([[40, 17, 39, 19],
       [61, 51, 15, 72],
       [56, 30, 69, 58],
       [85, 80, 80, 46],
       [50, 65, 14, 22],
       [72, 88, 43, 97],
       [89,  6, 83, 15],
       [10, 60, 70, 19],
       [11, 98, 46, 70],
       [64, 62, 67, 34]])
"""

 

 

자 이제 풀어보자

 

def make_scatter(): #문제1
    plt.xlabel('Englsh')
    plt.ylabel('Korea')
    plt.scatter(english_point,korea_point,marker='^')
    
make_scatter()

 

def make_plotbar(): #문제2
    fig, axs = plt.subplots(2, 2, figsize=(8, 6))

    axs[0, 0].hist(korea_point, bins=5)
    axs[0, 0].set_title('Korea')
    axs[0, 0].set_ylabel('Count')

    axs[0, 1].hist(english_point, bins=5)
    axs[0, 1].set_title('English')

    axs[1, 0].hist(math_point, bins=5)
    axs[1, 0].set_title('Math')
    axs[1, 0].set_ylabel('Count')

    axs[1, 1].hist(science_point, bins=5)
    axs[1, 1].set_title('Science')

    plt.tight_layout()

    plt.show()
    
make_plotbar()

 

 

 

def pie_chart(): #문제3
    grade = np.where(average_score>=90,'A',
                     np.where(average_score>=80,'B',
                              np.where(average_score>=70,'C','D')))
    unique, counts = np.unique(grade, return_counts=True)
    
    labels = unique  
    sizes = counts  

    plt.pie(sizes, labels=labels)
    plt.show()
    pie_chart()

 

 

 

def make_boxplot(): #문제4
    data = [korea_point,english_point,math_point,science_point]
    plt.boxplot(data, patch_artist=True, tick_labels=['Kor', 'Eng', 'Math', 'Science'],
                boxprops=dict(facecolor='none', color='black'))   
    plt.show()
    
make_boxplot()