FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

BUG: handle empty levels array in contour, closes #7486 by efiring · Pull Request #8719 · matplotlib/matplotlib · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
21 changes: 14 additions & 7 deletions lib/matplotlib/contour.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL