Skip to content

Small figsize yield wrong dimensions #2716

Closed as not planned
Closed as not planned

Description

@ibell

I am having issues with small figures that I am trying to generate. Here is a minimal working example that demonstrates the issue:

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(0.5,0.5))
plt.savefig('small.png',dpi = 100)

which yields a file small.png with dimensions 291 x 50 (wxh) so the height is being properly handled, but the width is not. This seems to be a bug. I seem to recall having similar issues with large figures, I can post another mwe for large figsize too.

Activity

  1. efiring commented on Jan 10, 2014

    @efiring
    Member

    I'm not seeing this.

    n [1]: import matplotlib.pyplot as plt
    
    In [2]: fig = plt.figure(figsize=(0.5,0.5))
    
    In [3]: plt.savefig('small.png',dpi = 100)
    
    In [4]: !file small.png
    small.png: PNG image data, 50 x 50, 8-bit/color RGBA, non-interlaced
    
    In [5]: import matplotlib
    
    In [6]: matplotlib.__version__
    Out[6]: u'1.4.x'
    
  2. ibell commented on Jan 10, 2014

    @ibell
    Author

    I'm on windows 7, MPL 1.3.0

    On Fri, Jan 10, 2014 at 8:10 PM, Eric Firing [email protected]:

    I'm not seeing this.

    n [1]: import matplotlib.pyplot as plt

    In [2]: fig = plt.figure(figsize=(0.5,0.5))

    In [3]: plt.savefig('small.png',dpi = 100)

    In [4]: !file small.png
    small.png: PNG image data, 50 x 50, 8-bit/color RGBA, non-interlaced

    In [5]: import matplotlib

    In [6]: matplotlib.version
    Out[6]: u'1.4.x'

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/2716#issuecomment-32055585
    .

  3. ibell commented on Jan 10, 2014

    @ibell
    Author

    Python 2.7.2

    On Fri, Jan 10, 2014 at 8:13 PM, Ian Bell [email protected] wrote:

    I'm on windows 7, MPL 1.3.0

    On Fri, Jan 10, 2014 at 8:10 PM, Eric Firing [email protected]:

    I'm not seeing this.

    n [1]: import matplotlib.pyplot as plt

    In [2]: fig = plt.figure(figsize=(0.5,0.5))

    In [3]: plt.savefig('small.png',dpi = 100)

    In [4]: !file small.png
    small.png: PNG image data, 50 x 50, 8-bit/color RGBA, non-interlaced

    In [5]: import matplotlib

    In [6]: matplotlib.version
    Out[6]: u'1.4.x'

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/2716#issuecomment-32055585
    .

  4. mdboom commented on Jan 13, 2014

    @mdboom
    Member

    Which backend? I can't reproduce with mpl 1.3.0/python 2.7.2 with the Agg backend on Linux.

  5. ibell commented on Jan 13, 2014

    @ibell
    Author

    This issue exists with WXAgg and WX backends, but Agg, TKAgg are fine.
    Those are the only backends I have that can spit out png. So seems like a
    wx related issue. I'm wx 2.8.12.1 as well

    FWIW, the small fig sizes work fine with WXAgg on OSX with 1.2.1

    On Jan 13, 2014, at 5:21 PM, Michael Droettboom [email protected]
    wrote:

    Which backend? I can't reproduce with mpl 1.3.0/python 2.7.2 with the Agg
    backend on Linux.

    —
    Reply to this email directly or view it on
    GitHubhttps://github.com//issues/2716#issuecomment-32184295
    .

  6. tacaswell commented on Jan 24, 2014

    @tacaswell
    Member

    There seems to be a lot of backend dependence here. Who is the local expert on Wx?

  7. efiring commented on Jan 24, 2014

    @efiring
    Member

    I don't think we have one among the core, but @wernerfb might be the one to step in; he has been working on phoenix compatibility.

  8. added this to the milestone on Jul 12, 2014
  9. petehuang commented on Dec 29, 2016

    @petehuang
    Contributor

    Hi,

    This issue has been inactive for a while. Can anyone repro using the details provided above? Thanks!

  10. tacaswell commented on Dec 29, 2016

    @tacaswell
    Member

    This is almost certainly due to the GUI maintaining some minimum size, (presumably for the toolbar?)

    Doing fig.set_size_inches((.5, .5), forward=False) after it is created, or if you do not need the GUI just using 'Agg' are probably the best work-around.

  11. tacaswell commented on Dec 29, 2016

    @tacaswell
    Member

    And I can reproduce this:

    16:51 $ ipython2
    Python 2.7.13 (default, Dec 21 2016, 07:16:46) 
    Type "copyright", "credits" or "license" for more information.
    
    IPython 5.1.0 -- An enhanced Interactive Python.
    ?         -> Introduction and overview of IPython's features.
    %quickref -> Quick reference.
    help      -> Python's own help system.
    object?   -> Details about 'object', use 'object??' for extra details.
    
    In [1]: import matplotlib
    
    In [2]: matplotlib.use('wxagg')
    
    In [3]: import matplotlib.pyplot as plt
    /usr/lib/python2.7/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
      warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')
    
    In [4]: plt.ion()
    
    In [5]: plt.figure(figsize=(.5, .5))
    Out[5]: <matplotlib.figure.Figure at 0x7fef6770a9d0>
    
    In [6]: fig = plt.gcf()
    
    In [7]: fig.get_size_inches()
    Out[7]: array([ 3.55,  0.5 ])
    
    In [8]: 
    
    
    
  12. pdhall99 commented on Mar 2, 2017

    @pdhall99

    I find the same problem with the TkAgg backend:

    Python 2.7.13 (default, Dec 18 2016, 05:36:31) 
    Type "copyright", "credits" or "license" for more information.
    
    IPython 5.1.0 -- An enhanced Interactive Python.
    ?         -> Introduction and overview of IPython's features.
    %quickref -> Quick reference.
    help      -> Python's own help system.
    object?   -> Details about 'object', use 'object??' for extra details.
    
    In [1]: import matplotlib
    
    In [2]: matplotlib.use('tkagg')
    
    In [3]: import matplotlib.pyplot as plt
    
    In [4]: plt.ion()
    
    In [5]: fig = plt.figure(figsize=(.5, .5))
    
    In [6]: fig.get_size_inches()
    Out[6]: array([ 2.2,  0.5])
    
    In [7]: plt.rcParams['toolbar'] = 'None'
    
    In [8]: fig_without_toolbar = plt.figure(figsize=(.5, .5))
    
    In [9]: fig_without_toolbar.get_size_inches()
    Out[9]: array([ 0.5,  0.5])
    

    Matplotlib version

    • Matplotlib 2.0.0, Python 2.7.13, OSX
    • Installed with MacPorts
  13. DietmarSchwertberger commented on Feb 18, 2018

    @DietmarSchwertberger
    Contributor

    I have had a look into this.
    For wx it's like this:

    • the figure is created with a size in pixels that is a result of the specified size and the configured dpi
    • the frame is created with the figure canvas being growable
    • the frame's layout manager ('Sizer') is called:
      • due to minimum size of window decoration, toolbar, statusbar etc. the frame is wider than the number of pixels required by the canvas
      • the figure canvas is resized to fill the available space
    • on WX: the _FigureCanvasWxBase._onSize event handler will set the figure size based on the actual size in pixels and the dpis

    So, the problem is a direct result of making the canvas resizable, even though a specified size plus dpi would result in a fixed size.
    There are several options:

    1. For the wx backend, it would probably be possible to make the canvas non-growable in the beginning and only allow growth later on. As side effect, the specified size would only be changed if the user resizes the window.
    2. Make GUI canvases non-resizable; maybe via an optional argument to plt.figure
    3. Always preserve the aspect ratio while filling the available space. This should be done on all GUI backends. Having the correct size for small figures would be a side effect of this, as usually the vertical size of the window is small.
    4. Always preserve the specified size in inches internally; use a zoom factor for the GUI window, either the same or different factors for x/y.

    Option 1) could be implemented for 2.2
    Anything else should be kept consistent over all backends and is more a topic for 3.0 I guess.

  14. DietmarSchwertberger commented on Feb 18, 2018

    @DietmarSchwertberger
    Contributor

    For wx this one would be a possible workaround:
    DietmarSchwertberger@4cd5499

    Example, after creation of the frame:
    workaround2716
    After a minimal resize by the user:
    workaround2716_resized

    P.S.: On e.g. Ubuntu, the frame sizing does not work correctly. That's because in the FigureFrameWx the toolbar is not set to the frame using SetToolBar, but added to the sizer (layout manager) instead to have it at the bottom instead of the top frame edge. This should be changed. Having the toolbar at the bottom seems not important to me. For a bottom toolbar, buttons should be used, not a wx.ToolBar.

    The window as opened:
    workaround2716_ubuntu
    The toolbar items are accessible via the pop-up:
    workaround2716_ubuntu2
    After resizing:
    workaround2716_ubuntu_resized

  15. oliversheridanmethven commented on Feb 19, 2018

    @oliversheridanmethven

    Is there anything in the rcParams which could be modified to fix this? (I can't see any canvas "re-size" or "preserve aspect_atio" options).

  16. modified the milestones: , needs sorting on Oct 6, 2022
  17. github-actions commented on Oct 9, 2023

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  18. added
    status: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.
    on Nov 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    GUI: wxstatus: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.status: inactiveMarked by the “Stale” Github Action

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions