2

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
Veedrac
  • 58,273
  • 15
  • 112
  • 169
morningstar2651
  • 179
  • 1
  • 6
  • Welcome to Stack Overflow! Please explain what you mean by "not working". Please include **full** tracebacks (if they exist) and a sample that is **small** and **runnable** *on its own* and that reproduces the problem. – Veedrac Jun 12 '14 at 23:15
  • Thank you Veedrac. I can't supply any of the data I'm working with, but I could create a dummy csv. There are no runtime errors. The script executes and generates plots, but it just doesn't generate the pdf files. – morningstar2651 Jun 12 '14 at 23:17
  • Dummy data is exactly the right solution, even if you could share the original data. ;) – Veedrac Jun 12 '14 at 23:20
  • Note that you're unlikely to actually need as much data as the original; there's little need for a loop if just running once demonstrates the problem (for example). – Veedrac Jun 12 '14 at 23:43
  • I've edited the OP - it now is small, runnable, and includes a link to a downloadable dummy csv. – morningstar2651 Jun 13 '14 at 00:13
  • It's working for me. I have a PDF generated. – Veedrac Jun 13 '14 at 00:23
  • Interesting. I'll test it with the dummy file and see if it works. If that doesn't work, I'll try it on a different machine. Thanks for the help. – morningstar2651 Jun 13 '14 at 00:48

0 Answers0