You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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'
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
.
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
.
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
.
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.
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]:
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])
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:
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.
Make GUI canvases non-resizable; maybe via an optional argument to plt.figure
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.
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.
Example, after creation of the frame:
After a minimal resize by the user:
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:
The toolbar items are accessible via the pop-up:
After resizing:
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!
Activity
efiring commentedon Jan 10, 2014
I'm not seeing this.
ibell commentedon 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]:
ibell commentedon Jan 10, 2014
Python 2.7.2
On Fri, Jan 10, 2014 at 8:13 PM, Ian Bell [email protected] wrote:
mdboom commentedon 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 commentedon 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 commentedon Jan 24, 2014
There seems to be a lot of backend dependence here. Who is the local expert on Wx?
efiring commentedon 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.
petehuang commentedon Dec 29, 2016
Hi,
This issue has been inactive for a while. Can anyone repro using the details provided above? Thanks!
tacaswell commentedon Dec 29, 2016
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 commentedon Dec 29, 2016
And I can reproduce this:
pdhall99 commentedon Mar 2, 2017
I find the same problem with the TkAgg backend:
Matplotlib version
DietmarSchwertberger commentedon Feb 18, 2018
I have had a look into this.
For wx it's like this:
_FigureCanvasWxBase._onSizeevent handler will set the figure size based on the actual size in pixels and the dpisSo, 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:
plt.figureOption 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 commentedon Feb 18, 2018
For wx this one would be a possible workaround:
DietmarSchwertberger@4cd5499
Example, after creation of the frame:


After a minimal resize by the user:
P.S.: On e.g. Ubuntu, the frame sizing does not work correctly. That's because in the
FigureFrameWxthe toolbar is not set to the frame usingSetToolBar, 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 awx.ToolBar.The window as opened:



The toolbar items are accessible via the pop-up:
After resizing:
oliversheridanmethven commentedon Feb 19, 2018
Is there anything in the
rcParamswhich could be modified to fix this? (I can't see any canvas "re-size" or "preserve aspect_atio" options).github-actions commentedon 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!