earthlab / matplotcheck

A python package for checking and testing matplotlib plots. We use this for autograding student assignments but there are many other potential use cases including package testing (for packages with plots)!
https://matplotcheck.readthedocs.io
BSD 3-Clause "New" or "Revised" License
18 stars 8 forks source link

Handling figures with multiple plots #48

Closed kysolvik closed 4 years ago

kysolvik commented 5 years ago

Currently matplotcheck can't easily handle figures with multiple plots (unless I'm missing something!). PlotTester accepts an axes object, so if you have multiple plots in same figure you would need to create multiple instances of PlotTester and test each.

For example, if we create a figure with two subplots and try to check the titles of both, the current set up just looks at the most recent axis. Could be set up to iterate over list of axes, or to run on a figure and its sub-axes.

Recreate issue:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotcheck.base import PlotTester

df = pd.DataFrame({"A": np.arange(100), "B": np.random.randint(0, 100, size=100)})

# Create figure with two subplots
fig, ax = plt.subplots(ncols=2)
df.plot("A", "B", kind="line", ax=ax[0])
df.plot("A", "B", kind="scatter", ax=ax[1])
ax[0].set_title("Line")
ax[1].set_title("Scatter")

# Create plot tester object from current axes
pt_line_scatter = PlotTester(plt.gca())

# Contains title will fail, doesn't find "Line" title
pt_line_scatter.assert_title_contains(["Line", "Scatter"], title_type="axes")
lwasser commented 5 years ago

ok i have thought about this a good bit. I think what we need to do here is to test this on some homework assignments. my guess is as we figure out how we need to iterate, we will create some helper wrapper functions to handle the iteration. but i think we need to implement it first manually a few times to understand the use cases better so that these wrappers can be generic enough to be broadly useful.

lwasser commented 5 years ago

update: forgot about the notebook module

ax1 = nb.convert_axes(plt) <- i think this will grab ALL axes but needs more testing.

lwasser commented 4 years ago

update: the way to handle subplots is to grab each ax object independently and then test against that. we should create a vignette that shows how to do this but i think we don't have to address any bugs in this case! i'll close this!