10000 Merge pull request #8574 from dstansby/contour-warning · matplotlib/matplotlib@dc6441f · GitHub
[go: up one dir, main page]

Skip to content

Commit dc6441f

Browse files
authored
Merge pull request #8574 from dstansby/contour-warning
Make sure circular contours don't throw a warning
2 parents 268420b + 844d725 commit dc6441f

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

lib/matplotlib/contour.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ def locate_label(self, linecontour, labelwidth):
334334
part of the contour).
335335
"""
336336

337+
# Number of contour points
337338
nsize = len(linecontour)
338339
if labelwidth > 1:
339340
xsize = int(np.ceil(nsize / labelwidth))
@@ -353,7 +354,10 @@ def locate_label(self, linecontour, labelwidth):
353354
xlast = XX[:, -1].reshape(xsize, 1)
354355
s = (yfirst - YY) * (xlast - xfirst) - (xfirst - XX) * (ylast - yfirst)
355356
L = np.sqrt((xlast - xfirst) ** 2 + (ylast - yfirst) ** 2).ravel()
356-
dist = np.add.reduce(([(abs(s)[i] / L[i]) for i in range(xsize)]), -1)
357+
# Ignore warning that divide by zero throws, as this is a valid option
358+
with np.errstate(divide='ignore', invalid='ignore'):
359+
dist = np.add.reduce([(abs(s)[i] / L[i]) for i in range(xsize)],
360+
-1)
357361
x, y, ind = self.get_label_coords(dist, XX, YY, ysize, labelwidth)
358362

359363
# There must be a more efficient way...
@@ -448,7 +452,10 @@ def calc_label_rot_and_inline(self, slc, ind, lw, lc=None, spacing=5):
448452

449453
# Round to integer values but keep as float
450454
# To allow check against nan below
451-
I = [np.floor(I[0]), np.ceil(I[1])]
455+
# Ignore nans here to avoid throwing an error on on Appveyor build
456+
# (can possibly be removed when build uses numpy 1.13)
457+
with np.errstate(invalid='ignore'):
458+
I = [np.floor(I[0]), np.ceil(I[1])]
452459

453460
# Actually break contours
454461
if closed:

lib/matplotlib/tests/test_contour.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,15 @@ def test_internal_cpp_api():
340340
with pytest.raises(ValueError) as excinfo:
341341
qcg.create_filled_contour(1, 0)
342342
excinfo.match(r'filled contour levels must be increasing')
343+
344+
345+
def test_circular_contour_warning():
346+
# Check that almost circular contours don't throw a warning
347+
with pytest.warns(None) as record:
348+
x, y = np.meshgrid(np.linspace(-2, 2, 4), np.linspace(-2, 2, 4))
349+
r = np.sqrt(x ** 2 + y ** 2)
350+
351+
plt.figure()
352+
cs = plt.contour(x, y, r)
353+
plt.clabel(cs)
354+
assert len(record) == 0

0 commit comments

Comments
 (0)
0