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

Skip to content

FIX: fail early for non-finite figure sizes #8647

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2017
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
FIX: fail early for non-finite figure sizes
Closes #8640
  • Loading branch information
tacaswell committed May 22, 2017
commit a4999acbbf6ebd6fa211f70becd49887dce663ab
4 changes: 2 additions & 2 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ def __setitem__(self, key, value):
super(_ColorMapping, self).__setitem__(key, value)
self.cache.clear()

def __delitem__(self, key, value):
super(_ColorMapping, self).__delitem__(key, value)
def __delitem__(self, key):
super(_ColorMapping, self).__delitem__(key)
self.cache.clear()


Expand Down
11 changes: 10 additions & 1 deletion lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,11 @@ 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))
self.bbox_inches = Bbox.from_bounds(0, 0, *figsize)

self.dpi_scale_trans = Affine2D().scale(dpi, dpi)
# do not use property as it will trigger
self._dpi = dpi
Expand Down Expand Up @@ -710,7 +714,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)):
Copy link
Contributor

Choose a reason for hiding this comment

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

np.isfinite([w, h]).all()

Copy link
Member Author

Choose a reason for hiding this comment

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

not convinced going through numpy is better in this case.

Copy link
Contributor

Choose a reason for hiding this comment

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

sounds fair

raise ValueError('figure size must be finite not '
'({}, {})'.format(w, h))
dpival = self.dpi
self.bbox_inches.p1 = w, h

Expand Down Expand Up @@ -920,6 +926,9 @@ def add_axes(self, *args, **kwargs):
raise ValueError(msg)
else:
rect = args[0]
if not np.isfinite(rect).all():
raise ValueError('all entries in rect must be finite '
'not {}'.format(rect))
projection_class, kwargs, key = process_projection_requirements(
self, *args, **kwargs)

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


def test_invalid_figure_size():
with pytest.raises(ValueError):
plt.figure(figsize=(1, np.nan))

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

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