8000 Merge pull request #13859 from timhoffm/positive-figsize · matplotlib/matplotlib@0f0ef9c · GitHub
[go: up one dir, main page]

Skip to content

Commit 0f0ef9c

Browse files
authored
Merge pull request #13859 from timhoffm/positive-figsize
Ensure figsize is positive finite
2 parents d9030ef + da83457 commit 0f0ef9c

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

lib/matplotlib/figure.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,9 @@ def __init__(self,
345345
if frameon is None:
346346
frameon = rcParams['figure.frameon']
347347

348-
if not np.isfinite(figsize).all():
349-
raise ValueError('figure size must be finite not '
350-
'{}'.format(figsize))
348+
if not np.isfinite(figsize).all() or (np.array(figsize) <= 0).any():
349+
raise ValueError('figure size must be positive finite not '
350+
f'{figsize}')
351351
self.bbox_inches = Bbox.from_bounds(0, 0, *figsize)
352352

353353
self.dpi_scale_trans = Affine2D().scale(dpi, dpi)
@@ -895,9 +895,9 @@ def set_size_inches(self, w, h=None, forward=True):
895895
# argument, so unpack them
896896
if h is None:
897897
w, h = w
898-
if not all(np.isfinite(_) for _ in (w, h)):
899-
raise ValueError('figure size must be finite not '
900-
'({}, {})'.format(w, h))
898+
size = w, h
899+
if not np.isfinite(size).all() or (np.array(size) <= 0).any():
900+
raise ValueError(f'figure size must be positive finite not {size}')
901901
self.bbox_inches.p1 = w, h
902902

903903
if forward:

lib/matplotlib/tests/test_figure.py

Lines changed: 12 additions & 3 deletions
< 8000 div data-testid="addition diffstat" class="DiffSquares-module__diffSquare--h5kjy DiffSquares-module__addition--jeNtt">
Original file line numberDiff line numberDiff line change
@@ -325,14 +325,23 @@ def test_change_dpi():
325325
assert fig.canvas.renderer.width == 200
326326

327327

328-
def test_invalid_figure_size():
328+
@pytest.mark.parametrize('width, height', [
329+
(1, np.nan),
330+
(0, 1),
331+
(-1, 1),
332+
(np.inf, 1)
333+
])
334+
def test_invalid_figure_size(width, height):
329335
with pytest.raises(ValueError):
330-
plt.figure(figsize=(1, np.nan))
336+
plt.figure(figsize=(width, height))
331337

332338
fig = plt.figure()
333339
with pytest.raises(ValueError):
334-
fig.set_size_inches(1, np.nan)
340+
fig.set_size_inches(width, height)
335341

342+
343+
def test_invalid_figure_add_axes():
344+
fig = plt.figure()
336345
with pytest.raises(ValueError):
337346
fig.add_axes((.1, .1, .5, np.nan))
338347

0 commit comments

Comments
 (0)
0