Title: [MNT]: Avoid using pyplot in tests · Issue #31079 · matplotlib/matplotlib · GitHub
Open Graph Title: [MNT]: Avoid using pyplot in tests · Issue #31079 · matplotlib/matplotlib
X Title: [MNT]: Avoid using pyplot in tests · Issue #31079 · matplotlib/matplotlib
Description: 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 est...
Open Graph Description: 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 cr...
X Description: 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 cr...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/31079
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[MNT]: Avoid using pyplot in tests","articleBody":"### Summary\n\nInspired by #31076: Can we remove the pyplot overhead from most tests?\n\nMany 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.\n\nAs a rough estimation, based on\n\n```\nIn [1]: import matplotlib.pyplot as plt\n\nIn [2]: from matplotlib.figure import Figure\n\nIn [3]: def bare_figure():\n ...: fig = Figure()\n ...: \n\nIn [4]: def pyplot_figure():\n ...: plt.figure()\n ...: plt.close()\n ...: \n\nIn [5]: %timeit bare_figure()\n250 μs ± 2.81 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)\n\nIn [6]: %timeit pyplot_figure()\n10.2 ms ± 493 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n```\nI 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.\n\nIf our test suite runs ~8min that'd be 3%. This is something but not not too much.\n\nSwitching would mean to replace\n```\nfig = plt.figure() --\u003e fig = Figure()\nfig, ax = plt.subplots() --\u003e ax = Figure().add_subplot() # assuming fig is not used, otherwise, split the commands.\n```\n\nI'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?\n\n### Proposed fix\n\nNone","author":{"url":"https://github.com/timhoffm","@type":"Person","name":"timhoffm"},"datePublished":"2026-02-04T21:55:25.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":7},"url":"https://github.com/31079/matplotlib/issues/31079"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:4c58bf9b-c9bd-7f55-bf20-990581a7aa0f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B4D6:17CF54:107E8F0:16ABBB9:6A53BE49 |
| html-safe-nonce | f879379476d0315be3831445fe140c816572a8ca80db328fefe9de555464a5a1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNEQ2OjE3Q0Y1NDoxMDdFOEYwOjE2QUJCQjk6NkE1M0JFNDkiLCJ2aXNpdG9yX2lkIjoiODM3MjI0NTc5MTI5MzgxNjM5MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 8c7468a7da1cabfc41f0c658f6526fd5cd3a4edcaf9a0a06444efb06f16ed2eb |
| hovercard-subject-tag | issue:3898593370 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/matplotlib/matplotlib/31079/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f98d11e71d54b3a81a6afc8389f857be4a6874dfd9d16d60a04e6711ddb9218e/matplotlib/matplotlib/issues/31079 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f98d11e71d54b3a81a6afc8389f857be4a6874dfd9d16d60a04e6711ddb9218e/matplotlib/matplotlib/issues/31079 |
| og:image:alt | 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 cr... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | timhoffm |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| go-import | github.com/matplotlib/matplotlib git https://github.com/matplotlib/matplotlib.git |
| octolytics-dimension-user_id | 215947 |
| octolytics-dimension-user_login | matplotlib |
| octolytics-dimension-repository_id | 1385122 |
| octolytics-dimension-repository_nwo | matplotlib/matplotlib |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1385122 |
| octolytics-dimension-repository_network_root_nwo | matplotlib/matplotlib |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width