Skip to content

[MNT]: Avoid using pyplot in tests #31079

@timhoffm

Description

@timhoffm

Summary

Inspired by #31076: Can we remove the pyplot overhead from most tests?

Many tests create figures. We have 1154 usages of plt.subplots() and 441 usages of plt.figure() in the tests. So we create around 1500 figures.

As a rough estimation, based on

In [1]: import matplotlib.pyplot as plt

In [2]: from matplotlib.figure import Figure

In [3]: def bare_figure():
   ...:     fig = Figure()
   ...: 

In [4]: def pyplot_figure():
   ...:     plt.figure()
   ...:     plt.close()
   ...: 

In [5]: %timeit bare_figure()
250 μs ± 2.81 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

In [6]: %timeit pyplot_figure()
10.2 ms ± 493 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)

I infer that the pyplot infrastructure for figure management costs around 10ms. This would mean, we can save around 15sec if we do not use pyplot.

If our test suite runs ~8min that'd be 3%. This is something but not not too much.

Switching would mean to replace

fig = plt.figure()  --> fig = Figure()
fig, ax = plt.subplots()  --> ax = Figure().add_subplot()  # assuming fig is not used, otherwise, split the commands.

I'm not clear it's worth the churn, but wanted to share the idea (even if it's just to document it and decide not to act on it). What do you think?

Proposed fix

None

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions