[Matplotlib-users] Grading matplotlib plots

Jess Hamrick jessica.b.hamrick at gmail.com
Fri Oct 9 03:25:05 CEST 2015

Hi all,

I am not sure how much this will interest people here, but given that it’s related to matplotlib I thought it was worth posting :-)

I have created a little utility package, called ‘plotchecker’, which helps instructors write autograder tests for matplotlib plots:

https://github.com/jhamrick/plotchecker <https://github.com/jhamrick/plotchecker>

More details:

I created this out of a need to be able to write autograder tests for matplotlib plots. However, it’s actually quite difficult to write autograder tests for matplotlib in a robust way, because there’s so many different ways of creating plots. For example, the following two ways of creating a scatter plot produce pretty much the same plot, but with different underlying representations of the data:

# method 1, creates a Line object
plt.plot(x, y, ‘o’)

# method 2, creats a Collection object
plt.scatter(x, y)

Or, in another example, you can create a plot and set the color to ‘k’, ‘black’, ‘#00000’, (0, 0, 0), or (0, 0, 0, 1). These are all exactly the same color, and valid ways of specifying “black”. But you shouldn’t have to manually test all these options if you want to check that students used the correct color.

So, I created plotchecker as an abstraction layer on top of matplotlib that will check all these things for you (e.g., `assert_x_data_equal(x)` regardless of whether `plt.plot` or `plt.scatter` was used, or `assert_colors_equal(‘black’)` instead of checking all possible versions of black). Currently, it supports line plots and scatter plots, and I have plans to support at least bar graphs and imshow/matshow plots as well.

Briefly, the way that it works is that you create a PlotChecker object (or ScatterPlotChecker or LinePlotChecker) and pass in the relevent Axes object to the constructor. Then, you call methods on the PlotChecker object to assert things about the that Axes object — e.g. if the data is correct, or the color is correct, or the axis limit are correct, etc.

If anyone has any feedback on this, I would love to hear it!

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20151008/79dd4405/attachment.html>

More information about the Matplotlib-users mailing list