Skip to content

better input validation on fill_between #7817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4757,6 +4757,7 @@ def fill_between(self, x, y1, y2=0, where=None, interpolate=False,
for filling between two sets of x-values

"""

if not rcParams['_internal.classic_mode']:
color_aliases = mcoll._color_aliases
kwargs = cbook.normalize_kwargs(kwargs, color_aliases)
Expand All @@ -4774,6 +4775,11 @@ def fill_between(self, x, y1, y2=0, where=None, interpolate=False,
y1 = ma.masked_invalid(self.convert_yunits(y1))
y2 = ma.masked_invalid(self.convert_yunits(y2))

for name, array in [('x', x), ('y1', y1), ('y2', y2)]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

if array.ndim > 1:
raise ValueError('Input passed into argument "%r"' % name +
'is not 1-dimensional.')

if y1.ndim == 0:
y1 = np.ones_like(x) * y1
if y2.ndim == 0:
Expand Down Expand Up @@ -4918,6 +4924,7 @@ def fill_betweenx(self, y, x1, x2=0, where=None,
for filling between two sets of y-values

"""

if not rcParams['_internal.classic_mode']:
color_aliases = mcoll._color_aliases
kwargs = cbook.normalize_kwargs(kwargs, color_aliases)
Expand All @@ -4934,6 +4941,11 @@ def fill_betweenx(self, y, x1, x2=0, where=None,
x1 = ma.masked_invalid(self.convert_xunits(x1))
x2 = ma.masked_invalid(self.convert_xunits(x2))

for name, array in [('y', y), ('x1', x1), ('x2', x2)]:
if array.ndim > 1:
raise ValueError('Input passed into argument "%r"' % name +
'is not 1-dimensional.')

if x1.ndim == 0:
x1 = np.ones_like(y) * x1
if x2.ndim == 0:
Expand Down
78 changes: 78 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,45 @@ def test_polycollection_joinstyle():
ax.set_ybound(0, 3)


@cleanup
def test_fill_between_2d_x_input():
x = np.zeros((2, 2))
y1 = 3
y2 = 3

fig = plt.figure()
ax = fig.add_subplot(211)
with pytest.raises(ValueError):
ax.plot(x, y1, x, y2, color='black')
ax.fill_between(x, y1, y2)


@cleanup
def test_fill_between_2d_y1_input():
x = np.arange(0.0, 2, 0.02)
y1 = np.zeros((2, 2))
y2 = 3

fig = plt.figure()
ax = fig.add_subplot(211)
with pytest.raises(ValueError):
ax.plot(x, y1, x, y2, color='black')
ax.fill_between(x, y1, y2)


@cleanup
def test_fill_between_2d_y2_input():
x = np.arange(0.0, 2, 0.02)
y1 = 3
y2 = np.zeros((2, 2))

fig = plt.figure()
ax = fig.add_subplot(211)
with pytest.raises(ValueError):
ax.plot(x, y1, x, y2, color='black')
ax.fill_between(x, y1, y2)


@image_comparison(baseline_images=['fill_between_interpolate'],
remove_text=True)
def test_fill_between_interpolate():
Expand Down Expand Up @@ -4801,6 +4840,45 @@ def test_tick_param_label_rotation():
assert text.get_rotation() == 90


@cleanup
def test_fill_betweenx_2d_y_input():
y = np.zeros((2, 2))
x1 = 3
x2 = 3

fig = plt.figure()
ax = fig.add_subplot(211)
with pytest.raises(ValueError):
ax.plot(y, x1, y, x2, color='black')
ax.fill_betweenx(y, x1, x2)


@cleanup
def test_fill_betweenx_2d_x1_input():
y = np.arange(0.0, 2, 0.02)
x1 = np.zeros((2, 2))
x2 = 3

fig = plt.figure()
ax = fig.add_subplot(211)
with pytest.raises(ValueError):
ax.plot(y, x1, y, x2, color='black')
ax.fill_betweenx(y, x1, x2)


@cleanup
def test_fill_betweenx_2d_x2_input():
y = np.arange(0.0, 2, 0.02)
x1 = 3
x2 = np.zeros((2, 2))

fig = plt.figure()
ax = fig.add_subplot(211)
with pytest.raises(ValueError):
ax.plot(y, x1, y, x2, color='black')
ax.fill_betweenx(y, x1, x2)


@cleanup(style='default')
def test_fillbetween_cycle():
fig, ax = plt.subplots()
Expand Down