A package for testing different types of matplotlib plots including:
- basic matplotlib plots
- geopandas spatial vector plots
- raster plots
- time series plots
- folium plots
and more!
There is often a need to test plots particularly when teaching data science courses. The Matplotlib api can be complex to navigate when trying to write tests. MatPlotCheck was developed to make it easier to test data, titles, axes and other elements of Matplotlib plots in support of both autograding and other testing needs.
MatPlotCheck was inspired by plotChecker developed by Jess Hamrick.
We spoke with her about our development and decided to extend plotChecker to suite some of the grading needs in our classes which include plots with spatial data using numpy for images and geopandas for vector data.
You can install MatPlotCheck using either pip or conda. To use pip run:
pip install --upgrade matplotcheck
To use conda:
conda install -c conda-forge matplotcheck
To import it into Python:
import matplotcheck as mpc
Matplotcheck is currently under significant development.
2D plot with x-axis label containing "x" and y-axis label containing "y" and "data"
from matplotcheck.cases import PlotBasicSuite
import pandas as pd
import unittest
axis = plt.gca()
data = pd.DataFrame(data={“x”:xvals, “y”:yvals})
suite = PlotBasicSuite(ax=axis, data_exp=data, xcol=”x”, ycol=”y”)
xlabel_contains=[“x”], ylabel_contains = [“y”,”data”])
results = unittest.TextTestRunner().run(suite)
Plot containing a spatial raster image and spatial polygon vector data
from matplotcheck.cases import PlotRasterSuite
axis = plt.gca()
suite = PlotRasterSuite(ax=axis, im_expected=image, polygons=polygons)
results = unittest.TextTestRunner().run(suite)
If you prefer to forgo the groupings into TestSuites, you can just use the assertions instead.
2D plot with x-axis label containing "x" and y-axis label containing "y" and "data"
from matplotcheck.base import PlotTester
import pandas as pd
axis = plt.gca()
pt = PlotTester(axis)
data = pd.DataFrame(data={“x”:xvals, “y”:yvals})
pt.assert_xydata(data, “x”, “y”)
pt.assert_xlabel_contains([“x”])
pt.assert_ylabel_contains([“y”, “data”])
Plot containing a spatial raster image and spatial polygon vector data
from matplotcheck.raster import RasterTester
from matplotcheck.vector import VectorTester
axis = plt.gca()
rt = RasterTester(axis)
vt = VectorTester(axis)
rt.assert_image(im_expected=image)
vt.assert_polygons(polygons_expected=polygons)
Caveats: This repo likely misses edge cases of the many ways matplotlib plots can be created. Please feel free to submit bugs!
We've welcome any and all contributions. Below are some of the contributors to MatPlotCheck.
We welcome contributions to MatPlotCheck! Please be sure to check out our contributing guidelines for more information about submitting pull requests or changes to MatPlotCheck.
MatPlotCheck citation information can be found on zenodo. A link to bibtext format is below: