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

Skip to content

Small figsize yield wrong dimensions #2716

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

Closed
ibell opened this issue Jan 10, 2014 · 15 comments
Closed

Small figsize yield wrong dimensions #2716

ibell opened this issue Jan 10, 2014 · 15 comments
Labels
GUI: wx status: closed as inactive Issues closed by the "Stale" Github Action. Please comment on any you think should still be open. status: inactive Marked by the “Stale” Github Action

Comments

@ibell
Copy link

ibell commented Jan 10, 2014

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.

@efiring
Copy link
Member

efiring commented Jan 10, 2014

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'

@ibell
Copy link
Author

ibell commented Jan 10, 2014

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
.

@ibell
Copy link
Author

ibell commented Jan 10, 2014

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
.

@mdboom
Copy link
Member

mdboom commented Jan 13, 2014

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

@ibell
Copy link
Author

ibell commented Jan 13, 2014

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
.

@tacaswell
Copy link
Member

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

@efiring
Copy link
Member

efiring commented Jan 24, 2014

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.

@tacaswell tacaswell added this to the unassigned milestone Jul 12, 2014
@petehuang
Copy link
Contributor

Hi,

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

@tacaswell
Copy link
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.

@tacaswell
Copy link
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]: 


@pdhall99
Copy link

pdhall99 commented Mar 2, 2017

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

@DietmarSchwertberger
Copy link
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.

@DietmarSchwertberger
Copy link
Contributor

DietmarSchwertberger commented Feb 18, 2018

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

@oliversheridanmethven
Copy link

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).

@github-actions
Copy link

github-actions bot commented Oct 9, 2023

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!

@github-actions github-actions bot added the status: inactive Marked by the “Stale” Github Action label Oct 9, 2023
@github-actions github-actions bot added the status: closed as inactive Issues closed by the "Stale" Github Action. Please comment on any you think should still be open. label Nov 8, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GUI: wx status: closed as inactive Issues closed by the "Stale" Github Action. Please comment on any you think should still be open. status: inactive Marked by the “Stale” Github Action
Projects
None yet
Development

No branches or pull requests

9 participants