8000 Merge pull request #15966 from jklymak/fix-check-subplot-syntax · matplotlib/matplotlib@e5283e0 · GitHub
[go: up one dir, main page]

Skip to content

Commit e5283e0

Browse files
authored
Merge pull request #15966 from jklymak/fix-check-subplot-syntax
FIX: check subplot kwargs
2 parents 5fb9709 + 67117cc commit e5283e0

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,12 @@ def __init__(self, fig, rect,
438438
self._sharex = sharex
439439
self._sharey = sharey
440440
if sharex is not None:
441+
if not isinstance(sharex, _AxesBase):
442+
raise TypeError('sharex must be an axes, not a bool')
441443
self._shared_x_axes.join(self, sharex)
442444
if sharey is not None:
445+
if not isinstance(sharey, _AxesBase):
446+
raise TypeError('sharey must be an axes, not a bool')
443447
self._shared_y_axes.join(self, sharey)
444448
self.set_label(label)
445449
self.set_figure(fig)

lib/matplotlib/pyplot.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,10 @@ def subplot(*args, **kwargs):
954954
cbook._warn_external("The subplot index argument to subplot() appears "
955955
"to be a boolean. Did you intend to use "
956956
"subplots()?")
957+
# Check for nrows and ncols, which are not valid subplot args:
958+
if 'nrows' in kwargs or 'ncols' in kwargs:
959+
raise TypeError("subplot() got an unexpected keyword argument 'ncols' "
960+
"and/or 'nrows'. Did you intend to call subplots()?")
957961

958962
fig = gcf()
959963
a = fig.add_subplot(*args, **kwargs)

lib/matplotlib/tests/test_axes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5489,6 +5489,13 @@ def test_shared_scale():
54895489
assert ax.get_xscale() == 'linear'
54905490

54915491

5492+
def test_shared_bool():
5493+
with pytest.raises(TypeError):
5494+
plt.subplot(sharex=True)
5495+
with pytest.raises(TypeError):
5496+
plt.subplot(sharey=True)
5497+
5498+
54925499
def test_violin_point_mass():
54935500
"""Violin plot should handle point mass pdf gracefully."""
54945501
plt.violinplot(np.array([0, 0]))

lib/matplotlib/tests/test_pyplot.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,10 @@ def test_pyplot_box():
5151
def test_stackplot_smoke():
5252
# Small smoke test for stackplot (see #12405)
5353
plt.stackplot([1, 2, 3], [1, 2, 3])
54+
55+
56+
def test_nrows_error():
57+
with pytest.raises(TypeError):
58+
plt.subplot(nrows=1)
59+
with pytest.raises(TypeError):
60+
plt.subplot(ncols=1)

0 commit comments

Comments
 (0)
0