Skip to content

BUG: handle empty levels array in contour, closes #7486 #8719

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 1 commit into from
Jun 14, 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
21 changes: 14 additions & 7 deletions lib/matplotlib/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -1132,14 +1132,10 @@ def _autolev(self, N):
self.locator = ticker.LogLocator()
else:
self.locator = ticker.MaxNLocator(N + 1, min_n_ticks=1)
zmax = self.zmax
zmin = self.zmin
lev = self.locator.tick_values(zmin, zmax)

lev = self.locator.tick_values(self.zmin, self.zmax)
self._auto = True
if self.filled:
return lev
# For line contours, drop levels outside the data range.
return lev[(lev > zmin) & (lev < zmax)]
return lev

def _contour_level_args(self, z, args):
"""
Expand All @@ -1165,6 +1161,17 @@ def _contour_level_args(self, z, args):
"Last {0} arg must give levels; see help({0})"
.format(fn))
self.levels = lev
else:
self.levels = np.asarray(self.levels).astype(np.float64)

if not self.filled:
inside = (self.levels > self.zmin) & (self.levels < self.zmax)
self.levels = self.levels[inside]
if len(self.levels) == 0:
self.levels = [self.zmin]
warnings.warn("No contour levels were found"
" within the data range.")

if self.filled and len(self.levels) < 2:
raise ValueError("Filled contours require at least 2 levels.")

Expand Down
22 changes: 22 additions & 0 deletions lib/matplotlib/tests/test_contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,28 @@ def test_contour_shape_invalid_2():
excinfo.match(r'Input z must be a 2D array.')


def test_contour_empty_levels():

x = np.arange(9)
z = np.random.random((9, 9))

fig, ax = plt.subplots()
with pytest.warns(UserWarning) as record:
ax.contour(x, x, z, levels=[])
assert len(record) == 1


def test_contour_uniform_z():

x = np.arange(9)
z = np.ones((9, 9))

fig, ax = plt.subplots()
with pytest.warns(UserWarning) as record:
ax.contour(x, x, z)
assert len(record) == 1


@image_comparison(baseline_images=['contour_manual_labels'])
def test_contour_manual_labels():

Expand Down