Я просматривал документацию для библиотеки matplot, но не понимаю, зачем нужна y_pos
. Например, почему я не могу использовать fullnames
вместо y_pos
?
def CreateScoreBarChart(subject):
#Get all the names and scores for that subject
GetScoresSQL="""SELECT firstname,secondname,score FROM students WHERE subject=%s"""
mycursor.execute(GetScoresSQL,(subject,))
myresults = mycursor.fetchall()
fullnames = []
scores = []
for person in myresults:
fullname=person[0]+" "+person[1]
fullnames.append(fullname)
scores.append(person[2])
y_pos = sorted(fullnames)
plt.bar(y_pos, scores, align='center', alpha=0.5,color=['b'])
plt.xticks(y_pos, fullnames)
plt.ylabel('Score')
plt.xlabel('Full Name')
plt.title(subject+"'s Class Scores")
plt.show()
Sample Data
#Function to populate database
def RandomStudentMaker(NoOfStudents):
studentFNames = ["Alex","Brad","Diane","Jimmy","Logan","Harry","Susan","Olivia","Beth","Amy","Charles","Megan","Gareth","Tony"]
studentSNames=["Gaston","Leslie","Copeland","Smith","Dill","Brown","Rowan","Austin","Harley","Eakin","Colgan","Fry","Cook","Laurie"]
subjectNames=["Chemistry","Biology","Computer Science"]
insertStudentSQL="INSERT INTO students (firstname,secondname,subject,score) VALUES(%s,%s,%s,%s)"
for i in range(0,NoOfStudents):
print("here")
mycursor.execute(insertStudentSQL,(random.choice(studentFNames),random.choice(studentSNames),random.choice(subjectNames),random.randint(0,100)))
mydb.commit()
RandomStudentMaker(20)