Title: Support subplots((m, n), ...) as shorthand for subplots(m, n, squeeze=False, ...) · Issue #19463 · matplotlib/matplotlib · GitHub
Open Graph Title: Support subplots((m, n), ...) as shorthand for subplots(m, n, squeeze=False, ...) · Issue #19463 · matplotlib/matplotlib
X Title: Support subplots((m, n), ...) as shorthand for subplots(m, n, squeeze=False, ...) · Issue #19463 · matplotlib/matplotlib
Description: Problem subplots() defaults to auto-squeezing the returned Axes array (returning a single array when one is requested, returning a 1D array when a single row or single column is requested), likely for practicality, but this makes dynamic...
Open Graph Description: Problem subplots() defaults to auto-squeezing the returned Axes array (returning a single array when one is requested, returning a 1D array when a single row or single column is requested), likely ...
X Description: Problem subplots() defaults to auto-squeezing the returned Axes array (returning a single array when one is requested, returning a 1D array when a single row or single column is requested), likely ...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/19463
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Support subplots((m, n), ...) as shorthand for subplots(m, n, squeeze=False, ...)","articleBody":"\u003c!--\r\nWelcome! Thanks for thinking of a way to improve Matplotlib.\r\n\r\n\r\nBefore creating a new feature request please search the issues for relevant feature requests.\r\n--\u003e\r\n\r\n### Problem\r\n\r\n`subplots()` defaults to auto-squeezing the returned Axes array (returning a single array when one is requested, returning a 1D array when a single row or single column is requested), likely for practicality, but this makes dynamic layouts pretty annoying: if you write `axs = fig.subplots(m, n)` where `m` and `n` are somehow dynamically computed (based on some dataset characteristics) and then later plan to use `axs[i, j]`, you basically have an IndexError waiting to happen unless you can be certain that `m` and `n` are never 1. Of course one can remember to always write `subplots(..., squeeze=False)`, but that's a bit verbose.\r\n\r\n### Proposed Solution\r\n\r\nMy preferred solution would probably have been to make the default `m` and `n` not 1, but None, so that None means \"1 in that direction, and squeeze that direction\" whereas 1 just means 1 (without squeezing). This would have fixed most common cases, e.g. if you write `subplots()` you get a single axes, `subplots(3)` or `subplots(nrows=3)` or `subplots(ncols=3)` you get a 1D array, `subplots(m, n)` (where `m` and `n` are dynamically computed variables) you always get a 2D array. I think in practice the main back-incompatibility of this proposal that occurs in real life is `subplots(1, 3)` (single row, likely not so uncommon), which would typically have to be written `subplots(None, 3)` or `subplots(ncols=3)` or `fig, (axs,) = subplots(3)` (for the additional unpacking).\r\n\r\nAssuming that this backcompat breakage is not acceptable (it likely is not), one alternative would be to change the signature of `subplots()` to also support taking a single *2-tuple* as argument (rather than `nrows` and `ncols` separately, and make `subplots((m, n))` never squeeze the input. (Likely we wouldn't bother supporting `m` or `n` being None, in that case.) Then it would be OK to retrain my muscle memory to just always add an extra pair of parentheses when calling `subplots`.\r\n\r\n### Additional context and prior art\r\n\r\n\u003c!-- Add any other context or screenshots about the feature request here. You can also include links to examples of other programs that have something similar to your request. For example:\r\n\r\n* Another project [...] solved this by [...]\r\n--\u003e","author":{"url":"https://github.com/anntzer","@type":"Person","name":"anntzer"},"datePublished":"2021-02-05T15:55:30.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":11},"url":"https://github.com/19463/matplotlib/issues/19463"}
| 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:4a60cd48-11d4-165c-00cc-d6c141cdf48c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AD1C:2F9C11:6D0591:912B19:6A541755 |
| html-safe-nonce | e9ffb194288cd8166b7c154500eb394abef629adfb795435f1efe3ff449f413e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRDFDOjJGOUMxMTo2RDA1OTE6OTEyQjE5OjZBNTQxNzU1IiwidmlzaXRvcl9pZCI6Ijg1Njg0OTM4NTkzNDQyMjYxMzMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 80523be8469478713aabc17407aa040be2c3c7f869ecf66c5efd2a3c10abf387 |
| hovercard-subject-tag | issue:802277265 |
| 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/19463/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a683cfccf31479ab864f8af4388adce4c59af2a0ca439ba9017644b37d3552b4/matplotlib/matplotlib/issues/19463 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a683cfccf31479ab864f8af4388adce4c59af2a0ca439ba9017644b37d3552b4/matplotlib/matplotlib/issues/19463 |
| og:image:alt | Problem subplots() defaults to auto-squeezing the returned Axes array (returning a single array when one is requested, returning a 1D array when a single row or single column is requested), likely ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | anntzer |
| 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