matplotlib icon indicating copy to clipboard operation
matplotlib copied to clipboard

Inconsistent image size with savefig(...,type='png') and TkAgg backend

Open sorenrasmussen opened this issue 8 years ago • 13 comments

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

sorenrasmussen avatar Apr 27 '17 11:04 sorenrasmussen

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.

tacaswell avatar Apr 27 '17 17:04 tacaswell

And this is one of the reasons we need a GUI backend enhancement: automatic scrollbars that allow preserving the requested size instead of resizing.

efiring avatar Apr 27 '17 18:04 efiring

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 avatar Jun 27 '18 13:06 BlGene

@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 avatar Jun 27 '18 13:06 efiring

@efiring: Thanks that solved the problem. :+1:

BlGene avatar Jun 27 '18 14:06 BlGene

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

oscargus avatar Jun 03 '22 20:06 oscargus

I've checked this and it works for my laptop. (matplotlib version 3.4.3)

BlGene avatar Jun 05 '22 09:06 BlGene

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.

oscargus avatar Jun 05 '22 11:06 oscargus

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[bot] avatar Jun 07 '23 02:06 github-actions[bot]

I can still reproduce this with the tk backend.

efiring avatar Jun 07 '23 07:06 efiring

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[bot] avatar Jun 10 '24 01:06 github-actions[bot]

I can not reproduce this, but I may be getting "lucky" due to my screen DPI.

tacaswell avatar Jun 10 '24 18:06 tacaswell

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[bot] avatar Jun 11 '25 02:06 github-actions[bot]