Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 62e8084

Browse files
authored
Merge pull request #8647 from tacaswell/fix_invalid_figsize
FIX: fail early for non-finite figure sizes
2 parents 6757719 + a4999ac commit 62e8084

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

lib/matplotlib/colors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ def __setitem__(self, key, value):
7979
super(_ColorMapping, self).__setitem__(key, value)
8080
self.cache.clear()
8181

82-
def __delitem__(self, key, value):
83-
super(_ColorMapping, self).__delitem__(key, value)
82+
def __delitem__(self, key):
83+
super(_ColorMapping, self).__delitem__(key)
8484
self.cache.clear()
8585

8686

lib/matplotlib/figure.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,11 @@ def __init__(self,
324324
if frameon is None:
325325
frameon = rcParams['figure.frameon']
326326

327+
if not np.isfinite(figsize).all():
328+
raise ValueError('figure size must be finite not '
329+
'{}'.format(figsize))
327330
self.bbox_inches = Bbox.from_bounds(0, 0, *figsize)
331+
328332
self.dpi_scale_trans = Affine2D().scale(dpi, dpi)
329333
# do not use property as it will trigger
330334
self._dpi = dpi
@@ -710,7 +714,9 @@ def set_size_inches(self, w, h=None, forward=True):
710714
# argument, so unpack them
711715
if h is None:
712716
w, h = w
713-
717+
if not all(np.isfinite(_) for _ in (w, h)):
718+
raise ValueError('figure size must be finite not '
719+
'({}, {})'.format(w, h))
714720
dpival = self.dpi
715721
self.bbox_inches.p1 = w, h
716722

@@ -920,6 +926,9 @@ def add_axes(self, *args, **kwargs):
920926
raise ValueError(msg)
921927
else:
922928
rect = args[0]
929+
if not np.isfinite(rect).all():
930+
raise ValueError('all entries in rect must be finite '
931+
'not {}'.format(rect))
923932
projection_class, kwargs, key = process_projection_requirements(
924933
self, *args, **kwargs)
925934

lib/matplotlib/tests/test_figure.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,15 @@ def test_change_dpi():
286286
fig.canvas.draw()
287287
assert fig.canvas.renderer.height == 200
288288
assert fig.canvas.renderer.width == 200
289+
290+
291+
def test_invalid_figure_size():
292+
with pytest.raises(ValueError):
293+
plt.figure(figsize=(1, np.nan))
294+
295+
fig = plt.figure()
296+
with pytest.raises(ValueError):
297+
fig.set_size_inches(1, np.nan)
298+
299+
with pytest.raises(ValueError):
300+
fig.add_axes((.1, .1, .5, np.nan))

0 commit comments

Comments
 (0)