Skip to content

Commit

Permalink
Merge pull request matplotlib#24912 from anntzer/cw
Browse files Browse the repository at this point in the history
Remove contour warning for "no-valid-levels".
  • Loading branch information
oscargus authored Jan 12, 2023
2 parents cf1b601 + fd6604d commit 018c5ef
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 45 deletions.
4 changes: 4 additions & 0 deletions doc/api/next_api_changes/behavior/24912-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
``contour`` no longer warns if no contour lines are drawn.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This can occur if the user explicitly passes a ``levels`` array with no values
between ``z.min()`` and ``z.max()``; or if ``z`` has the same value everywhere.
10 changes: 0 additions & 10 deletions lib/matplotlib/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -1137,18 +1137,8 @@ def _process_contour_level_args(self, args, z_dtype):
self.levels = self._autolev(levels_arg)
else:
self.levels = np.asarray(levels_arg, np.float64)

if not self.filled:
inside = (self.levels > self.zmin) & (self.levels < self.zmax)
levels_in = self.levels[inside]
if len(levels_in) == 0:
self.levels = [self.zmin]
_api.warn_external(
"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.")

if len(self.levels) > 1 and np.min(np.diff(self.levels)) <= 0.0:
raise ValueError("Contour levels must be increasing")

Expand Down
44 changes: 9 additions & 35 deletions lib/matplotlib/tests/test_contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,16 @@ def test_contour_shape_error(args, message):
ax.contour(*args)


def test_contour_empty_levels():

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

def test_contour_no_valid_levels():
fig, ax = plt.subplots()
with pytest.warns(UserWarning) as record:
ax.contour(x, x, z, levels=[])
assert len(record) == 1
# no warning for empty levels.
ax.contour(np.random.rand(9, 9), levels=[])
# no warning if levels is given and is not within the range of z.
cs = ax.contour(np.arange(81).reshape((9, 9)), levels=[100])
# ... and if fmt is given.
ax.clabel(cs, fmt={100: '%1.2f'})
# no warning if z is uniform.
ax.contour(np.ones((9, 9)))


def test_contour_Nlevels():
Expand All @@ -84,33 +85,6 @@ def test_contour_Nlevels():
assert (cs1.levels == cs2.levels).all()


def test_contour_badlevel_fmt():
# Test edge case from https://github.com/matplotlib/matplotlib/issues/9742
# User supplied fmt for each level as a dictionary, but Matplotlib changed
# the level to the minimum data value because no contours possible.
# This was fixed in https://github.com/matplotlib/matplotlib/pull/9743
x = np.arange(9)
z = np.zeros((9, 9))

fig, ax = plt.subplots()
fmt = {1.: '%1.2f'}
with pytest.warns(UserWarning) as record:
cs = ax.contour(x, x, z, levels=[1.])
ax.clabel(cs, fmt=fmt)
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(['contour_manual_labels'], remove_text=True, style='mpl20')
def test_contour_manual_labels():
x, y = np.meshgrid(np.arange(0, 10), np.arange(0, 10))
Expand Down

0 comments on commit 018c5ef

Please sign in to comment.