Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Description
Bug summary
When using0
or1
in place ofFalse
orTrue
insharex
orsharex
arguments ofpyplot.subplots
an error is raised.
Code for reproduction
importmatplotlib.pyplotaspltfig,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 usingsharex
instead ofsharey
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.pyindex 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 asTrue
andFalse
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