Home > Mobile >  How can I make this bar chart display number of students per grade
How can I make this bar chart display number of students per grade

Time:10-23

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import random
import seaborn as sns

def input_test_scores():
    #input for number of students
    studentnumber=int(input('Enter number of students'))
    #input for number of tests per student
    numberoftests=int(input('enter number of tests per student'))
    #empty list to store scores
    score=[]
        
    for i in range(studentnumber):
        #empty list for each students name and scores
         studentlist=[]
 
  
                 
                 
                 
        for j in range(numberoftests):
             #input for test score
             print("Enter score for test ", j 1, ":")
             #add students score to their list
             studentlist.append(int(input()))
             #add student list to score list to create a 2d list
         score.append(studentlist)
                 
     
     return score


def summarize_test_scores(score):
     

 
    mean=[]
    for data in score:
        m=sum(data)/len(data)
        mean.append(m)
    
    grade=[]
    for x in mean:
        if x >=90:
            grade.append('A')
        elif x >=80:
            grade.append('B')
        elif x >=70:
            grade.append('C')
        elif x >=60:
            grade.append('D')
        else:
             grade.append('F')
                     
    print("MEAN  GRADE")
    for i in range(len(score)):
        print(f'{mean[i]:.2f}  {grade[i]}')
    


#rolls = [random.randrange(1, 7) for i in range(600)]
#values, frequencies = np.unique(rolls, return_counts=True)

    title = f'Student Grades {len(grade):,} Times'
    sns.set_style('whitegrid')
    axes = sns.barplot(x=mean, y=grade, palette='bright')

                      

score=input_test_scores ()
summarize_test_scores(score)

This code is supposed to ask input for number of students, number of tests per student, and grades for each test, and then display number of students pet letter grade in a bar chart, displaying each letter even if no students earned that grade. Right now everything words except the bar chart displays grades letter on the y axis and grade mean on the x. What do I need to change?

CodePudding user response:

If I understand the question, you can try the following:

import collections

# ...

def summarize_test_scores(score):

    # ...

    grades_freq = collections.Counter(grade)
    sns.set_style('whitegrid')
    axes = sns.barplot(x=list(grades_freq.values()), y=list(grades_freq.keys()), palette='bright')

CodePudding user response:

Your code has the line:

axes = sns.barplot(x=mean, y=grade, palette='bright')

Whearas I'm assuming you want

axes = sns.barplot(x=grade, y=mean, palette='bright')

You simply got the axes wrong.

  • Related