Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,9 @@ def __init__(self,
if frameon is None:
frameon = rcParams['figure.frameon']

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

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

if forward:
Expand Down
15 changes: 12 additions & 3 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,23 @@ def test_change_dpi():
assert fig.canvas.renderer.width == 200


def test_invalid_figure_size():
@pytest.mark.parametrize('width, height', [
(1, np.nan),
(0, 1),
(-1, 1),
(np.inf, 1)
])
def test_invalid_figure_size(width, height):
with pytest.raises(ValueError):
plt.figure(figsize=(1, np.nan))
plt.figure(figsize=(width, height))

fig = plt.figure()
with pytest.raises(ValueError):
fig.set_size_inches(1, np.nan)
fig.set_size_inches(width, height)


def test_invalid_figure_add_axes():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need two blank lines here, which is what travis is failing on.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style fixed.

fig = plt.figure()
with pytest.raises(ValueError):
fig.add_axes((.1, .1, .5, np.nan))

Expand Down