Title: [Bug]: sharex and sharey don't accept 0 and 1 as bool values · Issue #24349 · matplotlib/matplotlib · GitHub
Open Graph Title: [Bug]: sharex and sharey don't accept 0 and 1 as bool values · Issue #24349 · matplotlib/matplotlib
X Title: [Bug]: sharex and sharey don't accept 0 and 1 as bool values · Issue #24349 · matplotlib/matplotlib
Description: Bug summary When using 0 or 1 in place of False or True in sharex or sharex arguments of pyplot.subplots an error is raised. Code for reproduction import matplotlib.pyplot as plt fig, ax = plt.subplots(ncols=2,sharey=1) Actual outcome We...
Open Graph Description: Bug summary When using 0 or 1 in place of False or True in sharex or sharex arguments of pyplot.subplots an error is raised. Code for reproduction import matplotlib.pyplot as plt fig, ax = plt.subp...
X Description: Bug summary When using 0 or 1 in place of False or True in sharex or sharex arguments of pyplot.subplots an error is raised. Code for reproduction import matplotlib.pyplot as plt fig, ax = plt.subp...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/24349
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Bug]: sharex and sharey don't accept 0 and 1 as bool values","articleBody":"### Bug summary\r\n\r\nWhen using `0` or `1` in place of `False` or `True` in `sharex` or `sharex` arguments of `pyplot.subplots` an error is raised.\r\n\r\n### Code for reproduction\r\n\r\n```python\r\nimport matplotlib.pyplot as plt\r\n\r\nfig, ax = plt.subplots(ncols=2,sharey=1)\r\n```\r\n\r\n\r\n### Actual outcome\r\n\r\nWe get the following error : \r\n```\r\nTraceback (most recent call last):\r\n File \"/***/shareyArg.py\", line 3, in \u003cmodule\u003e\r\n fig, ax = plt.subplots(ncols=2,sharey=1)\r\n File \"/***/matplotlib/lib/matplotlib/pyplot.py\", line 1448, in subplots\r\n axs = fig.subplots(nrows=nrows, ncols=ncols, sharex=sharex, sharey=sharey,\r\n File \"/***/matplotlib/lib/matplotlib/figure.py\", line 889, in subplots\r\n axs = gs.subplots(sharex=sharex, sharey=sharey, squeeze=squeeze,\r\n File \"/***/matplotlib/lib/matplotlib/gridspec.py\", line 293, in subplots\r\n _api.check_in_list([\"all\", \"row\", \"col\", \"none\"],\r\n File \"/***/matplotlib/lib/matplotlib/_api/__init__.py\", line 131, in check_in_list\r\n raise ValueError(msg)\r\nValueError: 1 is not a valid value for sharey; supported values are 'all', 'row', 'col', 'none'\r\n```\r\n\r\nNote that using `sharex` instead of `sharey` produces the same error (albeit with the following warning :\r\n```\r\nUserWarning: sharex argument to subplots() was an integer. Did you intend to use subplot() (without 's')?\r\n```\r\nbut this is expected and not part of the present issue)\r\n\r\n### Expected outcome\r\n\r\nI expected values 1 and 0 to be understood as bool.\r\n\r\n\r\n\r\n### Additional information\r\n\r\nSuggested fix : \r\n\r\n```patch\r\ndiff --git a/lib/matplotlib/gridspec.py b/lib/matplotlib/gridspec.py\r\nindex 06dd3f19f6..32ee7c306e 100644\r\n--- a/lib/matplotlib/gridspec.py\r\n+++ b/lib/matplotlib/gridspec.py\r\n@@ -276,9 +276,9 @@ class GridSpecBase:\r\n raise ValueError(\"GridSpec.subplots() only works for GridSpecs \"\r\n \"created with a parent figure\")\r\n \r\n- if isinstance(sharex, bool):\r\n+ if isinstance(sharex, bool) or sharex == 1 or sharex == 0:\r\n sharex = \"all\" if sharex else \"none\"\r\n- if isinstance(sharey, bool):\r\n+ if isinstance(sharey, bool) or sharey == 1 or sharey == 0:\r\n sharey = \"all\" if sharey else \"none\"\r\n # This check was added because it is very easy to type\r\n # `subplots(1, 2, 1)` when `subplot(1, 2, 1)` was intended.\r\n```\r\n\r\nMaybe not accepting 1 or 0 was done on purpose, but I did not find it very clear from the error message as `True` and `False` are accepted but not listed. \r\n\r\nI am happy to chat about an other fix, if this one doesn't do the trick. I can also create a PR in case you think this fix is good enough !\r\n\r\n### Operating system\r\n\r\nLinux 5.10.0-19-amd64 #1 SMP Debian 5.10.149-2\r\n\r\n### Matplotlib Version\r\n\r\n3.7.0.dev600+g0b6d3703ff\r\n\r\n### Matplotlib Backend\r\n\r\nTkAgg\r\n\r\n### Python version\r\n\r\n3.10.0\r\n\r\n### Jupyter version\r\n\r\nNot applicable\r\n\r\n### Installation\r\n\r\ngit checkout","author":{"url":"https://github.com/vdbma","@type":"Person","name":"vdbma"},"datePublished":"2022-11-03T11:16:44.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/24349/matplotlib/issues/24349"}
| 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:df179890-96b4-b9bc-a98d-0d5c30088b83 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8FB2:155482:15FBD:1B3A4:6A5254DD |
| html-safe-nonce | 0117bfd8cf1516e7e103261923e6944d98260018db7f4c98f077a66ee520acf8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RkIyOjE1NTQ4MjoxNUZCRDoxQjNBNDo2QTUyNTRERCIsInZpc2l0b3JfaWQiOiI3MDE5ODg1MzgwMDU4MTA5MTQ5IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 2e8f86f17b14118f846353256569e3bcc118909a32990409306a3c5e0ef285ff |
| hovercard-subject-tag | issue:1434491202 |
| 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/24349/issue_layout |
| twitter:image | https://opengraph.githubassets.com/495103bb587dbc40d9d7e2ba0b313194d961996217bad37f2921732937df7125/matplotlib/matplotlib/issues/24349 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/495103bb587dbc40d9d7e2ba0b313194d961996217bad37f2921732937df7125/matplotlib/matplotlib/issues/24349 |
| og:image:alt | Bug summary When using 0 or 1 in place of False or True in sharex or sharex arguments of pyplot.subplots an error is raised. Code for reproduction import matplotlib.pyplot as plt fig, ax = plt.subp... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | vdbma |
| 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 | 7aed05249554b889eb33d002851a973eebcc7e91 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width