FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[Bug]: sharex and sharey don't accept 0 and 1 as bool values · Issue #24349 · matplotlib/matplotlib · GitHub

Repository navigation

[Bug]: sharex and sharey don't accept 0 and 1 as bool values #24349

Closed

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 get the following error :

Traceback (most recent call last):
  File "/***/shareyArg.py", line 3, in <module>
    fig, ax = plt.subplots(ncols=2,sharey=1)
  File "/***/matplotlib/lib/matplotlib/pyplot.py", line 1448, in subplots
    axs = fig.subplots(nrows=nrows, ncols=ncols, sharex=sharex, sharey=sharey,
  File "/***/matplotlib/lib/matplotlib/figure.py", line 889, in subplots
    axs = gs.subplots(sharex=sharex, sharey=sharey, squeeze=squeeze,
  File "/***/matplotlib/lib/matplotlib/gridspec.py", line 293, in subplots
    _api.check_in_list(["all", "row", "col", "none"],
  File "/***/matplotlib/lib/matplotlib/_api/__init__.py", line 131, in check_in_list
    raise ValueError(msg)
ValueError: 1 is not a valid value for sharey; supported values are 'all', 'row', 'col', 'none'

Note that using sharex instead of sharey produces the same error (albeit with the following warning :

UserWarning: sharex argument to subplots() was an integer.  Did you intend to use subplot() (without 's')?

but this is expected and not part of the present issue)

Expected outcome

I expected values 1 and 0 to be understood as bool.

Additional information

Suggested fix :

diff --git a/lib/matplotlib/gridspec.py b/lib/matplotlib/gridspec.py
index 06dd3f19f6..32ee7c306e 100644
--- a/lib/matplotlib/gridspec.py
+++ b/lib/matplotlib/gridspec.py
@@ -276,9 +276,9 @@ class GridSpecBase:
             raise ValueError("GridSpec.subplots() only works for GridSpecs "
                              "created with a parent figure")
 
-        if isinstance(sharex, bool):
+        if isinstance(sharex, bool) or sharex == 1 or sharex == 0:
             sharex = "all" if sharex else "none"
-        if isinstance(sharey, bool):
+        if isinstance(sharey, bool) or sharey == 1 or sharey == 0:
             sharey = "all" if sharey else "none"
         # This check was added because it is very easy to type
         # `subplots(1, 2, 1)` when `subplot(1, 2, 1)` was intended.

Maybe 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.

I 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 !

Operating system

Linux 5.10.0-19-amd64 #1 SMP Debian 5.10.149-2

Matplotlib Version

3.7.0.dev600+g0b6d3703ff

Matplotlib Backend

TkAgg

Python version

3.10.0

Jupyter version

Not applicable

Installation

git checkout

Activity

  1. jklymak commented on Nov 3, 2022

    Member

    I think both your suggestions are reasonable. We should probably accept 0 and 1 and we should improve the error messages to indicate booleans are acceptable. Any interest in trying a Pull Request?

  2. vdbma commented on Nov 3, 2022

    ContributorAuthor

    Thanks for the reply !
    I have a personal fork ready for a Pull Request, I'd gladly try !

  3. added this to the v3.7.0 milestone on Nov 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL