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

Skip to content

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

Closed
@mmokrejs

Description

@mmokrejs

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

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