Skip to content

Commit 506bd99

Browse files
committed
Add input validation for fill_between and fill_betweenx and unit tests
1 parent b1f4820 commit 506bd99

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4757,6 +4757,13 @@ def fill_between(self, x, y1, y2=0, where=None, interpolate=False,
47574757
for filling between two sets of x-values
47584758
47594759
"""
4760+
4761+
for input in [('x', x), ('y1', y1), ('y2', y2)]:
4762+
if not (cbook.is_scalar(input[1]) or
4763+
cbook.is_scalar(cbook.safe_first_element(input[1]))):
4764+
raise ValueError('Input passed into argument "' + input[0] +
4765+
'" is not 1-dimensional.')
4766+
47604767
if not rcParams['_internal.classic_mode']:
47614768
color_aliases = mcoll._color_aliases
47624769
kwargs = cbook.normalize_kwargs(kwargs, color_aliases)
@@ -4918,6 +4925,12 @@ def fill_betweenx(self, y, x1, x2=0, where=None,
49184925
for filling between two sets of y-values
49194926
49204927
"""
4928+
4929+
for input in [('y', y), ('x1', x1), ('x2', x2), ('where', where)]:
4930+
if not cbook.is_scalar(cbook.safe_first_element(input[1])):
4931+
raise ValueError('Input passed into argument "' + input[0] +
4932+
'" is not 1-dimensional.')
4933+
49214934
if not rcParams['_internal.classic_mode']:
49224935
color_aliases = mcoll._color_aliases
49234936
kwargs = cbook.normalize_kwargs(kwargs, color_aliases)

lib/matplotlib/tests/test_axes.py

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

727727

728+
@raises(ValueError)
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+
ax.plot(x, y1, x, y2, color='black')
737+
ax.fill_between(x, y1, y2)
738+
739+
740+
@raises(ValueError)
741+
def test_fill_between_2d_y1_input():
742+
x = np.arange(0.0, 2, 0.02)
743+
y1 = np.zeros((2, 2))
744+
y2 = 3
745+
746+
fig = plt.figure()
747+
ax = fig.add_subplot(211)
748+
ax.plot(x, y1, x, y2, color='black')
749+
ax.fill_between(x, y1, y2)
750+
751+
752+
@raises(ValueError)
753+
def test_fill_between_2d_y2_input():
754+
x = np.arange(0.0, 2, 0.02)
755+
y1 = 3
756+
y2 = np.zeros((2, 2))
757+
758+
fig = plt.figure()
759+
ax = fig.add_subplot(211)
760+
ax.plot(x, y1, x, y2, color='black')
761+
ax.fill_between(x, y1, y2)
762+
763+
728764
@image_comparison(baseline_images=['fill_between_interpolate'],
729765
remove_text=True)
730766
def test_fill_between_interpolate():
@@ -4801,6 +4837,42 @@ def test_tick_param_label_rotation():
48014837
assert text.get_rotation() == 90
48024838

48034839

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

0 commit comments

Comments
 (0)