这里@virxen:
import numpy as np
import matplotlib.pyplot as plt
from FinalGradeFunction import computeFinalGrades
from loaddatafunction import dataLoad
data=dataLoad('data.csv')
df1= data.drop(['StudentID', 'Name'], axis=1)
grades=df1.to_numpy()
grades is in this case=array([[ 7, 7, 4],
[12, 10, 10],
[-3, 7, 2],
[10, 12, 12],
[ 7, 7, 5],
[12, 10, 10],
[-3, 7, 2],
[10, 12, 12]], dtype=int64)
def gradesPlot(grades):
# gradesPlot is a function which generates two plots.
#one which is a bar plot and the other is a graph.
# Usage: gradesPlot(grades)
#
# Input:the grades of the students given as an NxM matrix
# containing the grades on the 7-step-scale given to N students on M different assignments
# Output grades (two plots).
#
# Authors: Yaren Kart, 2021.
#Plot1 Find Grades
#Counting the occurrences of the grades from the 7-step-scale:
#creating a vector made up of the number of zeros equivalent to the length of the Final_Grades.
Final_Grades=computeFinalGrades(grades)
v=np.zeros(len(Final_Grades))
#counting how many times the different grades occur and putting the values in the vector v.
for i in range(len(v)):
v[i]=sum(Final_Grades==i+1)
#Ploting the bar plot:
#ploting the figure.
fig1 = plt.figure()
#determining the space between the bars.
ax = fig1.add_axes([0,0,1.5,1.5])
#setting the 7-step-scale.
x = ['-3', '00', '02', '4','7','10','12']
#creating the bar plot.
ax.bar(x,v)
#labelling the x-axis.
plt.xlabel('The 7-step-scale grades')
# labeling the y-axis.
plt.ylabel('The number of students')
#giving the bar plot a title.
plt.title('The distribution of final grades')
plt.savefig('Finalgrades',bbox_inches='tight')
#-----------------------------------------------------------------------------------------------------------------------------------------------
#Plot2 "grades per assignment"
fig2=plt.figure()
#will move the x- and y-values so that the dots wont overlap
jitter_min = -0.1
jitter_max = 0.1
#creating a for loop
#iterate over the grades array, one row at a time.
#enumerate returns an index - a, and the row.
for a,row in enumerate(grades):
x = (a*np.ones(len(row)) + (jitter_max-jitter_min)*np.random.random(size=len(row))+jitter_min)
plt.plot(x, row, 'o', label='Assignment {:1d}'.format(a), clip_on=False)
plt.xlabel('Assignments')
plt.ylabel('Grades')
plt.title('The distribution of grades per assignment)
#putting legends next to the graph.
plt.legend(loc='center left',prop={"size":10},bbox_to_anchor=(1, 0.5))
y=''
x=''
X=[]
Y=[]
#Creating a for loop
for i in range(len(grades)):
y=np.array([np.mean(grades[:,i],axis=0)])
x=np.array([i+1])
#Adding all the x values into X and all the y-values into Y.
X.append(x)
Y.append(y)
plt.plot(X,Y,label='The average of the assignments.')
#assigning labels and a title to the graph.
plt.xlabel('Assignments')
plt.ylabel('Grades')
plt.title('The distribution of grades per assignment')
#putting legends next to the graph.
plt.legend(loc='center left',prop={"size":10},bbox_to_anchor=(1, 0.5))
#plt.savefig('Gradesperassignment',bbox_inches='tight')
plt.savefig('Gradesperassignment',bbox_inches='tight')
print(gradesPlot(grades))