Search code examples
pythonmatplotlibgraphgraphingplotly

Using Python to graph student progress


I'm experimenting with python graphing for the first time and I want try what I've learned by graphing some of my student's progress. My progress data is in a table in a format the same as what I have mocked up below. I Have used MSPaint (sorry) to mock up what I think would be a decent graph to show them their progress.

What is the right name for this type of graph and what would be the first steps to achieve it? I can't see anything quite like it on http://matplotlib.org/ or on https://plot.ly/

Please feel free to tell me I am laying out the graph all wrong. Quick MSpaint mockup


Solution

  • I took a stab at generating your example chart in matplotlib. I suspect others with stronger matplotlib-foo could greatly improve this :)

    import matplotlib.pyplot as plt
    import numpy as np
    
    students = ['steve', 'bob', 'ralph']
    progress = [
    [1, 3, 4, 4, 5],
    [2, 3, 4, 4, 5],
    [3, 3, 4, 5, 5]]
    
    (fig, ax) = plt.subplots(1, 1)
    
    # Offset lines by some fraction of one
    dx = 1.0 / len(progress)
    xoff = dx / 2.0
    for i, (name, data) in enumerate(zip(students, progress)):
      ax.plot(np.arange(len(data)) + xoff, data, label=name, marker='o')
      xoff += dx
    
    ax.set_xticks(np.arange(0, len(progress[0]) + 0.01, dx), minor=True)
    ax.set_xticks(np.arange(1, len(progress[0])+1))
    labels = students * len(progress[0])
    week = 1
    for i,l in enumerate(labels):
      if l == students[1]:
        # hack to add Week label below the second label for each block
        labels[i] = "%s\nWeek %s" % (l, week)
        week += 1
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    
    ax.set_xticklabels(labels, fontsize=8, ha='left', minor=True)
    ax.set_xticklabels([])
    ax.tick_params(which='both', direction = 'out')
    ax.tick_params(axis='x', which='major', width=4)
    ax.tick_params(axis='x', which='major', length=7)
    ax.tick_params(axis='y', which='major', width=0, length=0)
    
    ax.set_ylim(0, 6)
    ax.set_yticks(range(1, 6))
    
    ax.get_xaxis().tick_bottom()
    ax.get_yaxis().tick_left()
    
    ax.set_title("Student Progress")
    
    ax.legend(loc='best')
    
    fig.show()
    

    example figure