Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 19a830e

Browse files
authored
Merge pull request #7817 from samsontmr/fix-#7510
[MRG+1] better input validation on `fill_between`
2 parents 7c16415 + e702cd8 commit 19a830e

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4757,6 +4757,7 @@ def fill_between(self, x, y1, y2=0, where=None, interpolate=False,
47574757
for filling between two sets of x-values
47584758
47594759
"""
4760+
47604761
if not rcParams['_internal.classic_mode']:
47614762
color_aliases = mcoll._color_aliases
47624763
kwargs = cbook.normalize_kwargs(kwargs, color_aliases)
@@ -4774,6 +4775,11 @@ def fill_between(self, x, y1, y2=0, where=None, interpolate=False,
47744775
y1 = ma.masked_invalid(self.convert_yunits(y1))
47754776
y2 = ma.masked_invalid(self.convert_yunits(y2))
47764777

4778+
for name, array in [('x', x), ('y1', y1), ('y2', y2)]:
4779+
if array.ndim > 1:
4780+
raise ValueError('Input passed into argument "%r"' % name +
4781+
'is not 1-dimensional.')
4782+
47774783
if y1.ndim == 0:
47784784
y1 = np.ones_like(x) * y1
47794785
if y2.ndim == 0:
@@ -4918,6 +4924,7 @@ def fill_betweenx(self, y, x1, x2=0, where=None,
49184924
for filling between two sets of y-values
49194925
49204926
"""
4927+
49214928
if not rcParams['_internal.classic_mode']:
49224929
color_aliases = mcoll._color_aliases
49234930
kwargs = cbook.normalize_kwargs(kwargs, color_aliases)
@@ -4934,6 +4941,11 @@ def fill_betweenx(self, y, x1, x2=0, where=None,
49344941
x1 = ma.masked_invalid(self.convert_xunits(x1))
49354942
x2 = ma.masked_invalid(self.convert_xunits(x2))
49364943

4944+
for name, array in [('y', y), ('x1', x1), ('x2', x2)]:
4945+
if array.ndim > 1:
4946+
raise ValueError('Input passed into argument "%r"' % name +
4947+
'is not 1-dimensional.')
4948+
49374949
if x1.ndim == 0:
49384950
x1 = np.ones_like(y) * x1
49394951
if x2.ndim == 0:

lib/matplotlib/tests/test_axes.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,45 @@ def test_polycollection_joinstyle():
725725
ax.set_ybound(0, 3)
726726

727727

728+
@cleanup
729+
def test_fill_between_2d_x_input():
730+
x = np.zeros((2, 2))
731+
y1 = 3
732+
y2 = 3
733+
734+
fig = plt.figure()
735+
ax = fig.add_subplot(211)
736+
with pytest.raises(ValueError):
737+
ax.plot(x, y1, x, y2, color='black')
738+
ax.fill_between(x, y1, y2)
739+
740+
741+
@cleanup
742+
def test_fill_between_2d_y1_input():
743+
x = np.arange(0.0, 2, 0.02)
744+
y1 = np.zeros((2, 2))
745+
y2 = 3
746+
747+
fig = plt.figure()
748+
ax = fig.add_subplot(211)
749+
with pytest.raises(ValueError):
750+
ax.plot(x, y1, x, y2, color='black')
751+
ax.fill_between(x, y1, y2)
752+
753+
754+
@cleanup
755+
def test_fill_between_2d_y2_input():
756+
x = np.arange(0.0, 2, 0.02)
757+
y1 = 3
758+
y2 = np.zeros((2, 2))
759+
760+
fig = plt.figure()
761+
ax = fig.add_subplot(211)
762+
with pytest.raises(ValueError):
763+
ax.plot(x, y1, x, y2, color='black')
764+
ax.fill_between(x, y1, y2)
765+
766+
728767
@image_comparison(baseline_images=['fill_between_interpolate'],
729768
remove_text=True)
730769
def test_fill_between_interpolate():
@@ -4801,6 +4840,45 @@ def test_tick_param_label_rotation():
48014840
assert text.get_rotation() == 90
48024841

48034842

4843+
@cleanup
4844+
def test_fill_betweenx_2d_y_input():
4845+
y = np.zeros((2, 2))
4846+
x1 = 3
4847+
x2 = 3
4848+
4849+
fig = plt.figure()
4850+
ax = fig.add_subplot(211)
4851+
with pytest.raises(ValueError):
4852+
ax.plot(y, x1, y, x2, color='black')
4853+
ax.fill_betweenx(y, x1, x2)
4854+
4855+
4856+
@cleanup
4857+
def test_fill_betweenx_2d_x1_input():
4858+
y = np.arange(0.0, 2, 0.02)
4859+
x1 = np.zeros((2, 2))
4860+
x2 = 3
4861+
4862+
fig = plt.figure()
4863+
ax = fig.add_subplot(211)
4864+
with pytest.raises(ValueError):
4865+
ax.plot(y, x1, y, x2, color='black')
4866+
ax.fill_betweenx(y, x1, x2)
4867+
4868+
4869+
@cleanup
4870+
def test_fill_betweenx_2d_x2_input():
4871+
y = np.arange(0.0, 2, 0.02)
4872+
x1 = 3
4873+
x2 = np.zeros((2, 2))
4874+
4875+
fig = plt.figure()
4876+
ax = fig.add_subplot(211)
4877+
with pytest.raises(ValueError):
4878+
ax.plot(y, x1, y, x2, color='black')
4879+
ax.fill_betweenx(y, x1, x2)
4880+
4881+
48044882
@cleanup(style='default')
48054883
def test_fillbetween_cycle():
48064884
fig, ax = plt.subplots()

0 commit comments

Comments
 (0)