What I'm trying to do is build a python script that plots letter-grade distributions by class, and emails each instructor with a PDF of the distributions for all of their classes.
The email part of the script works fine, as does the generation of the plots. What isn't working is that a PDF file isn't being created.
import pandas as pd
import numpy as np
import scipy
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
import csv
# Import ALL grading data
print "Reading distribution.csv..."
df = pd.read_csv('distribution.csv', header=0)
# Generate the mailing list
mailing_list = df.email.unique()
# Build the PDFs
for email in mailing_list:
pp = PdfPages('grading_distribution.pdf')
filtered_df = df[df.email == email]
# Generate a course list
course_list = filtered_df.course.unique()
for course in course_list:
fig = plt.figure()
filtered_df[filtered_df.course == course].groupby('letter_grade').enrollment.sum().plot(kind='bar')
plt.title(str(course) + " Grading Distribution")
plt.xlabel("Letter Grade")
plt.ylabel("Number of Students")
fig.savefig(pp, format='pdf')
pp.close()
distribution.csv:
term,course,email,letter_grade,enrollment
Summer 2014,ABC123,jdoe@gmail.com,A,1
Summer 2014,ABC123,jdoe@gmail.com,B,1
Summer 2014,ABC123,jdoe@gmail.com,B,1
Summer 2014,ARR111,jdoe@gmail.com,D,1
Summer 2014,ARR111,jdoe@gmail.com,C,1
Summer 2014,ARR111,jdoe@gmail.com,F,1
Summer 2014,XYZ987,abob@gmail.com,F,1
Summer 2014,XYZ987,abob@gmail.com,C,1
Summer 2014,XYZ987,abob@gmail.com,C,1
Summer 2014,ABC123,jdoe@gmail.com,B,1
Summer 2014,ABC123,jdoe@gmail.com,C,1
Summer 2014,ABC123,jdoe@gmail.com,C,1
Summer 2014,ARR111,jdoe@gmail.com,D,1
Summer 2014,ARR111,jdoe@gmail.com,C,1
Summer 2014,ARR111,jdoe@gmail.com,A,1
Summer 2014,XYZ987,abob@gmail.com,A,1
Summer 2014,XYZ987,abob@gmail.com,F,1
Summer 2014,XYZ987,abob@gmail.com,C,1