Skip to content

Commit 6b3c6b9

Browse files
authored
Backport of #11917 (#13583)
* Backport from master
1 parent 152936d commit 6b3c6b9

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

lib/matplotlib/contour.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import six
88
from six.moves import xrange
99

10+
from numbers import Integral
1011
import warnings
12+
1113
import matplotlib as mpl
1214
import numpy as np
1315
from numpy import ma
@@ -1206,21 +1208,15 @@ def _contour_level_args(self, z, args):
12061208
self._auto = False
12071209
if self.levels is None:
12081210
if len(args) == 0:
1209-
lev = self._autolev(7)
1211+
levels_arg = 7 # Default, hard-wired.
12101212
else:
1211-
level_arg = args[0]
1212-
try:
1213-
if type(level_arg) == int:
1214-
lev = self._autolev(level_arg)
1215-
else:
1216-
lev = np.asarray(level_arg).astype(np.float64)
1217-
except:
1218-
raise TypeError(
1219-
"Last {0} arg must give levels; see help({0})"
1220-
.format(fn))
1221-
self.levels = lev
1213+
levels_arg = args[0]
1214+
else:
1215+
levels_arg = self.levels
1216+
if isinstance(levels_arg, Integral):
1217+
self.levels = self._autolev(levels_arg)
12221218
else:
1223-
self.levels = np.asarray(self.levels).astype(np.float64)
1219+
self.levels = np.asarray(levels_arg).astype(np.float64)
12241220

12251221
if not self.filled:
12261222
inside = (self.levels > self.zmin) & (self.levels < self.zmax)

lib/matplotlib/tests/test_contour.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,17 @@ def test_contour_empty_levels():
131131
assert len(record) == 1
132132

133133

134+
def test_contour_Nlevels():
135+
# A scalar levels arg or kwarg should trigger auto level generation.
136+
# https://github.com/matplotlib/matplotlib/issues/11913
137+
z = np.arange(12).reshape((3, 4))
138+
fig, ax = plt.subplots()
139+
cs1 = ax.contour(z, 5)
140+
assert len(cs1.levels) > 1
141+
cs2 = ax.contour(z, levels=5)
142+
assert (cs1.levels == cs2.levels).all()
143+
144+
134145
def test_contour_badlevel_fmt():
135146
# test funny edge case from
136147
# https://github.com/matplotlib/matplotlib/issues/9742

0 commit comments

Comments
 (0)