Skip to content

Creating axes and figures with NaN sizes should raise errors earlier #8640

@ngoldbaum

Description

@ngoldbaum

Right now matplotlib only objects to creating a figure or axes with NaN dimensions as the plot is saved to disk (or in my case simply seg faults).

Code for reproduction

from matplotlib import pyplot as plt
import numpy as np

fsize = [10.85, np.float64('nan')]
figure = plt.figure(figsize=fsize)
axrect = (0.11059907834101382, np.float64('nan'),
          0.73732718894009219, np.float64('nan'))
ax = figure.add_axes(axrect)
ax.plot([1, 2, 3], [1, 2, 3])

plt.savefig('test.png')

Actual outcome

This script produces the following traceback:

Traceback (most recent call last):
  File "test.py", line 11, in <module>
    plt.savefig('test.png')
  File "/usr/local/lib/python3.5/site-packages/matplotlib/pyplot.py", line 697, in savefig
    res = fig.savefig(*args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/figure.py", line 1573, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/backend_bases.py", line 2252, in print_figure
    **kwargs)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/backends/backend_agg.py", line 545, in print_png
    FigureCanvasAgg.draw(self)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/backends/backend_agg.py", line 464, in draw
    self.figure.draw(self.renderer)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/artist.py", line 63, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/figure.py", line 1144, in draw
    renderer, self, dsu, self.suppressComposite)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/image.py", line 139, in _draw_list_compositing_images
    a.draw(renderer)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/artist.py", line 63, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/axes/_base.py", line 2426, in draw
    mimage._draw_list_compositing_images(renderer, self, dsu)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/image.py", line 139, in _draw_list_compositing_images
    a.draw(renderer)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/artist.py", line 63, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/axis.py", line 1136, in draw
    ticks_to_draw = self._update_ticks(renderer)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/axis.py", line 969, in _update_ticks
    tick_tups = [t for t in self.iter_ticks()]
  File "/usr/local/lib/python3.5/site-packages/matplotlib/axis.py", line 969, in <listcomp>
    tick_tups = [t for t in self.iter_ticks()]
  File "/usr/local/lib/python3.5/site-packages/matplotlib/axis.py", line 912, in iter_ticks
    majorLocs = self.major.locator()
  File "/usr/local/lib/python3.5/site-packages/matplotlib/ticker.py", line 1786, in __call__
    return self.tick_values(vmin, vmax)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/ticker.py", line 1794, in tick_values
    locs = self._raw_ticks(vmin, vmax)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/ticker.py", line 1736, in _raw_ticks
    nbins = max(min(self.axis.get_tick_space(), 9),
  File "/usr/local/lib/python3.5/site-packages/matplotlib/axis.py", line 2025, in get_tick_space
    return int(np.floor(length / size))
ValueError: cannot convert float NaN to integer

Unfortunately I wasn't able to produce a simple test script, but in the context of a larger application this error triggered a segmentation fault in python. Here's the traceback from faulthandler for that more complicated case:

Fatal Python error: Segmentation fault

Current thread 0x00007fff98e643c0 (most recent call first):
  File "/usr/local/lib/python3.5/site-packages/cycler.py", line 227 in __iter__
  File "/usr/local/lib/python3.5/site-packages/cycler.py", line 373 in by_key
  File "/usr/local/lib/python3.5/site-packages/matplotlib/colors.py", line 138 in to_rgba
  File "/usr/local/lib/python3.5/site-packages/matplotlib/patches.py", line 317 in _set_facecolor
  File "/usr/local/lib/python3.5/site-packages/matplotlib/patches.py", line 436 in set_fill
  File "/usr/local/lib/python3.5/site-packages/matplotlib/patches.py", line 130 in __init__
  File "/usr/local/lib/python3.5/site-packages/matplotlib/spines.py", line 54 in __init__
  File "/usr/local/lib/python3.5/site-packages/matplotlib/spines.py", line 469 in linear_spine
  File "/usr/local/lib/python3.5/site-packages/matplotlib/axes/_base.py", line 960 in _gen_axes_spines
  File "/usr/local/lib/python3.5/site-packages/matplotlib/axes/_base.py", line 526 in __init__
  File "/usr/local/lib/python3.5/site-packages/matplotlib/figure.py", line 930 in add_axes
  File "/Users/goldbaum/Documents/yt-git/yt/visualization/base_plot_types.py", line 95 in __init__
  File "/Users/goldbaum/Documents/yt-git/yt/visualization/base_plot_types.py", line 193 in __init__
  File "/Users/goldbaum/Documents/yt-git/yt/visualization/plot_window.py", line 1835 in __init__
  File "/Users/goldbaum/Documents/yt-git/yt/visualization/plot_window.py", line 863 in _setup_plots
  File "/Users/goldbaum/Documents/yt-git/yt/visualization/plot_window.py", line 231 in __init__
  File "/Users/goldbaum/Documents/yt-git/yt/visualization/plot_window.py", line 678 in __init__
  File "/Users/goldbaum/Documents/yt-git/yt/visualization/plot_window.py", line 1342 in __init__
  File "/Users/goldbaum/Documents/yt-git/yt/visualization/plot_window.py", line 2025 in SlicePlot
  File "test.py", line 5 in <module>

Keep in mind faulthandler tracebacks go the opposite direction from regular python tracebacks - the seg fault was happening in cycler of all places!

Expected outcome

An error message earlier in the script warning about the NaNs in either fsize or axrect.

Matplotlib version

  • Operating System: MacOS
  • Matplotlib Version: 2.0.2
  • Python Version: 3.5.2
  • Jupyter Version (if applicable):
  • Other Libraries:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions