Title: Idea: Reducing verbosity through more compact API patterns · Issue #30941 · matplotlib/matplotlib · GitHub
Open Graph Title: Idea: Reducing verbosity through more compact API patterns · Issue #30941 · matplotlib/matplotlib
X Title: Idea: Reducing verbosity through more compact API patterns · Issue #30941 · matplotlib/matplotlib
Description: Summary Matplotlib is often considered verbose. I've been pondering whether/how one could write the code more compact without completely reinventing our API. The two main points for simplification I've come up with are: Don't use set_* f...
Open Graph Description: Summary Matplotlib is often considered verbose. I've been pondering whether/how one could write the code more compact without completely reinventing our API. The two main points for simplification ...
X Description: Summary Matplotlib is often considered verbose. I've been pondering whether/how one could write the code more compact without completely reinventing our API. The two main points for simplificat...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/30941
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Idea: Reducing verbosity through more compact API patterns","articleBody":"### Summary\n\nMatplotlib is often considered verbose. I've been pondering whether/how one could write the code more compact without completely reinventing our API. The two main points for simplification I've come up with are:\n\n- Don't use `set_*` for configuration, but instead do the configuration via kwargs. The `set_*` methods can be a fallback for mor complex configuration.\n- Don't hold a figure reference as it's rarely needed.\n\nWhat I explicitly do not want to change:\n- No breakage of existing API\n- No completely new interface namespace\n- No method chaining\n\nThis is just an early and wild idea, not a proposal. Thoughts and feedback are welcome.\n\n\n\n\n\n### Proposed fix\n\nThe ideas are illustrated on a slightly modified variant of https://matplotlib.org/devdocs/gallery/lines_bars_and_markers/simple_plot.html\n\nThe formal Axes interface approach is:\n\n```python\nfig, ax = plt.subplots()\nax.plot(t, s)\nax.set_xlabel('time (s)')\nax.set_ylabel('voltage (mV)')\nax.set_title('A simple plot')\nplt.show()\n```\n\nIn some parts, we already have replaced the individual setters by `ax.set(...)` with kwargs.\n```python\nfig, ax = plt.subplots()\nax.plot(t, s)\nax.set(xlabel='time (s)', ylabel='voltage (mV)', title='A simple plot')\nplt.show()\n```\n\n*Possible improvement 1:* Replace `ax.set(...)` by kwargs during Axes creation:\n```python\nfig, ax = plt.subplots(xlabel='time (s)', ylabel='voltage (mV)', title='A simple plot')\nax.plot(t, s)\nplt.show()\n```\n\n*Possible improvement 2:* Don't store a reference to the figure - rationale: The `fig, ax = plt.subplots()` pattern is quite boilerplate and you rarely need the figure. One can always get the figure via `ax.get_figure()` if needed, which is only mildy more work than holding a `fig` variable.\n\n```python\nax = plt.figure().subplots(xlabel='time (s)', ylabel='voltage (mV)', title='A simple plot')\nax.plot(t, s)\nplt.show()\n```\nWhat is still not so nice is that you still need the `plt.figure().subplots(` chaining for the simple single-Axes case. While \n```python\nax = plt.axes(xlabel='time (s)', ylabel='voltage (mV)', title='A simple plot')\n```\nworks with the same result, there's a caveat in that `plt.axes()` reuses an existing Figure if possible whereas `plt.figure().subplot()` always creates a new figure. That's a potential footgun and we may want to think whether there's a way around this. Otherwise\n```python\nax = plt.axes(xlabel='time (s)', ylabel='voltage (mV)', title='A simple plot')\nax.plot(t, s)\nplt.show()\n```\nwould look quite clean to me:\n- create an Axes with some properties set\n- add data\n- show\n\nIn jupyter notebooks you'd even not need the show and could (as an abbreviation, not as a universally recommended pattern) create the plot in one line:\n```python\nplt.axes(xlabel='time (s)', ylabel='voltage (mV)', title='A simple plot').plot(t, s)\n```\n\n\n\n\n","author":{"url":"https://github.com/timhoffm","@type":"Person","name":"timhoffm"},"datePublished":"2026-01-07T14:24:56.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":7},"url":"https://github.com/30941/matplotlib/issues/30941"}
| 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:c08d6acd-0eee-93a0-faa1-9805b411e6b0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C696:8ADEB:64ABA8:88B633:6969DD9C |
| html-safe-nonce | 14a065cb822ce61ab2fde88ace349a10d15088c1ba47308c99060c649d2a03df |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNjk2OjhBREVCOjY0QUJBODo4OEI2MzM6Njk2OUREOUMiLCJ2aXNpdG9yX2lkIjoiNTU5OTU5NTM0MDg1MTk2MTI0NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 4a6f467403b10c621cfb9b1187558afcf5516d23167680a6640c0b05bf223763 |
| hovercard-subject-tag | issue:3788983412 |
| 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/30941/issue_layout |
| twitter:image | https://opengraph.githubassets.com/71c727aaed313364a086f9b9fc5c1e8961c1d8fb31589041ff0d251899c2ef66/matplotlib/matplotlib/issues/30941 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/71c727aaed313364a086f9b9fc5c1e8961c1d8fb31589041ff0d251899c2ef66/matplotlib/matplotlib/issues/30941 |
| og:image:alt | Summary Matplotlib is often considered verbose. I've been pondering whether/how one could write the code more compact without completely reinventing our API. The two main points for simplification ... |
| 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 | 7b32f1c7c4549428ee399213e8345494fc55b5637195d3fc5f493657579235e8 |
| 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 | bdde15ad1b403e23b08bbd89b53fbe6bdf688cad |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width