Skip to content
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

Document figure.get_size_inches, improve set_size_inches and improve a ValueError message #2303

Closed
mmokrejs opened this issue Aug 16, 2013 · 2 comments
Milestone

Comments

@mmokrejs
Copy link

It appeared in the thread http://matplotlib.1069221.n5.nabble.com/RendererAgg-int-width-int-height-dpi-debug-False-ValueError-width-and-height-must-each-be-below-32768-td27756.html that figure.get_size_inches() returns unexpetedly a view of a numpy array. Views behave unexpectedly to users not familiar with numpy.

  1. The http://matplotlib.org/api/figure_api.html?highlight=get_size_inches#matplotlib.figure.Figure.get_size_inches needs to be written. Special care needs to be to expain what Ben wrote in the thread:
The bug here is assuming that DefaultSize still contained the values you printed earlier. This is subtle (and I missed it before), but what you are getting back from F.get_size_inches() is a view of the internal numpy array. When you set the new size, the internal array was updated, not replaced. This is much in the same vein as Python mutables, but taken a bit further than you are probably used to. Because the internal array was updated, the view (stored in DefaultSize) showed the new data as well. So, when you tried to set (what you thought was still) the original size, it was merely setting the current values back to itself. Therefore, no change.

So, to force DefaultSize to be immutable, just cast it as a tuple:

DefaultSize = tuple(F.get_size_inches())

  1. A link to get_size_inches() should be introduced from set_size_inches(), that is where users are likely to hit this issue.
    http://matplotlib.org/api/figure_api.html?highlight=set_size_inches#matplotlib.figure.Figure.set_size_inches

  2. Once users sets a wrong figure size or something gets screwed (don't know the cause), a stacktrace like this is returned. Please improve the error message to provide more details about what has the user passed so bad to matplotlib. This is from mpl-1.2.1

    File "/usr/lib64/python2.7/site-packages/matplotlib/figure.py", line 1362, in savefig
    self.canvas.print_figure(_args, *_kwargs)
    File "/usr/lib64/python2.7/site-packages/matplotlib/backend_bases.py", line 2096, in print_figure
    **kwargs)
    File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 492, in print_png
    FigureCanvasAgg.draw(self)
    File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 435, in draw
    self.renderer = self.get_renderer()
    File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 454, in get_renderer
    self.renderer = RendererAgg(w, h, self.figure.dpi)
    File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 85, in init
    self._renderer = _RendererAgg(int(width), int(height), dpi, debug=False)
    ValueError: width and height must each be below 32768

@mmokrejs
Copy link
Author

Sorry, the numbering got screwed. Should be points 1, 2 and 3.

@mmokrejs
Copy link
Author

In relation to the stacktrace I think the source problem is that I set figsize to [ 11.2 520.8]. There should be a sanity check in figure.set_size_inches() so that matplotlib does not crash much later while saving a figure. It's too late.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants