Skip to content

Commit fd6604d

Browse files
committed
Remove contour warning for "no-valid-levels".
If the user explicitly passes a levels array (the default is auto-determined), let's assume that they know what they are doing.
1 parent 7bc69f2 commit fd6604d

File tree

3 files changed

+13
-45
lines changed

3 files changed

+13
-45
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
``contour`` no longer warns if no contour lines are drawn.
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
This can occur if the user explicitly passes a ``levels`` array with no values
4+
between ``z.min()`` and ``z.max()``; or if ``z`` has the same value everywhere.

lib/matplotlib/contour.py

-10
Original file line numberDiff line numberDiff line change
@@ -1137,18 +1137,8 @@ def _process_contour_level_args(self, args, z_dtype):
11371137
self.levels = self._autolev(levels_arg)
11381138
else:
11391139
self.levels = np.asarray(levels_arg, np.float64)
1140-
1141-
if not self.filled:
1142-
inside = (self.levels > self.zmin) & (self.levels < self.zmax)
1143-
levels_in = self.levels[inside]
1144-
if len(levels_in) == 0:
1145-
self.levels = [self.zmin]
1146-
_api.warn_external(
1147-
"No contour levels were found within the data range.")
1148-
11491140
if self.filled and len(self.levels) < 2:
11501141
raise ValueError("Filled contours require at least 2 levels.")
1151-
11521142
if len(self.levels) > 1 and np.min(np.diff(self.levels)) <= 0.0:
11531143
raise ValueError("Contour levels must be increasing")
11541144

lib/matplotlib/tests/test_contour.py

+9-35
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,16 @@ def test_contour_shape_error(args, message):
6262
ax.contour(*args)
6363

6464

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

7576

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

8687

87-
def test_contour_badlevel_fmt():
88-
# Test edge case from https://github.com/matplotlib/matplotlib/issues/9742
89-
# User supplied fmt for each level as a dictionary, but Matplotlib changed
90-
# the level to the minimum data value because no contours possible.
91-
# This was fixed in https://github.com/matplotlib/matplotlib/pull/9743
92-
x = np.arange(9)
93-
z = np.zeros((9, 9))
94-
95-
fig, ax = plt.subplots()
96-
fmt = {1.: '%1.2f'}
97-
with pytest.warns(UserWarning) as record:
98-
cs = ax.contour(x, x, z, levels=[1.])
99-
ax.clabel(cs, fmt=fmt)
100-
assert len(record) == 1
101-
102-
103-
def test_contour_uniform_z():
104-
105-
x = np.arange(9)
106-
z = np.ones((9, 9))
107-
108-
fig, ax = plt.subplots()
109-
with pytest.warns(UserWarning) as record:
110-
ax.contour(x, x, z)
111-
assert len(record) == 1
112-
113-
11488
@image_comparison(['contour_manual_labels'], remove_text=True, style='mpl20')
11589
def test_contour_manual_labels():
11690
x, y = np.meshgrid(np.arange(0, 10), np.arange(0, 10))

0 commit comments

Comments
 (0)