Inconsistent image size with savefig(...,type='png') and TkAgg backend
Bug report
Closed #8542 and opened this slightly modified version which better illustrates the problem.
Bug summary
When using the TK backend, savefig() with type='png' produces different image sizes, depending on the dpi argument when calling figure().
Code for reproduction
from pylab import *
%matplotlib tk
figsize = (10/3.0, 1)
plt.figure(figsize=figsize,dpi=100)
plt.savefig("100-300dpi.png",type="png",dpi=300)
plt.figure(figsize=figsize,dpi=96) #96 is my screen DPI
plt.savefig("96-300dpi.png",type="png",dpi=300)
Actual outcome
100-300dpi.png is 999x300 pixels. 96-300dpi.png is 1000x300 pixels.
Expected outcome
I would expect both to come out at 1000x300 pixels.
Matplotlib version
- Matplotlib 2.0.0
- Python 2.7.13
- IPython 5.1.0
- Jupyter Version: 4.4.1
- Platform: Linux
ah, this makes sense now. The GUI backends resize them selves to fit the screen, apparently inconsistently between backends.
At 100 dpi you are asking Matplotlib to render a figure which in 333.3333333... pixels wide, which we drop to 333 pixels (and re-size the figure size inches to 3.33 accordingly so that all of the internal math machinery works). Going back up to 300 dpi, you get 999 pixels
At 96 dpi, the size in 320 pixels so we do not resize the figure and you get the 1000 pixels you expect in the final image.
And this is one of the reasons we need a GUI backend enhancement: automatic scrollbars that allow preserving the requested size instead of resizing.
There is also a slight size inconsistency when saving several figures for videos. This occurs when using the same setting to save the images, namely:
self.fig = plt.figure(figsize = (2,2))
self.fig.savefig(fn, bbox_inches='tight', pad_inches=0)
My script is located here: https://gist.github.com/BlGene/7a2585ed3726cd08ae536aea43493db4 These are example file sizes that I get:
000.png: PNG image data, 294 x 229, 8-bit/color RGBA, non-interlaced
001.png: PNG image data, 282 x 228, 8-bit/color RGBA, non-interlaced
002.png: PNG image data, 276 x 228, 8-bit/color RGBA, non-interlaced
@BlGene Using `bbox_inches='tight' does not make sense when saving images for a video. It is adjusting the size independently for each frame, so slight changes in tick labels, titles, etc. will cause the size changes you observe.
@efiring: Thanks that solved the problem. :+1:
I tried this now and not sure if my screen DPI makes it work, but both images are 1000 x 300. If someone else can please try, this should be possible to close (given that it works for more people).
I've checked this and it works for my laptop. (matplotlib version 3.4.3)
I tried it on my ArchLinux machine in Spyder (and hence the qt5 backend) and there it is still 999x300 pixels, so I will not close it.
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!
I can still reproduce this with the tk backend.
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!
I can not reproduce this, but I may be getting "lucky" due to my screen DPI.
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!