Skip to content

Reliably set the output image size #15363

Description

@ppwwyyxx

Bug report

I would like to use matplotlib to render a figure, and convert the figure to RGB images. I expect the output image to be of certain pixel resolution W x H.

Many online resources (eg.. https://stackoverflow.com/questions/13714454/specifying-and-saving-a-figure-with-exact-size-in-pixels) suggests the following, but works most of the time but does not work in the following case:
Code for reproduction

import matplotlib as mpl
import matplotlib.figure as mplfigure
from matplotlib.backends.backend_agg import FigureCanvasAgg
width = 956
height = 928
fig = mplfigure.Figure(frameon=False)
dpi = fig.get_dpi()
print(dpi)
fig.set_size_inches(width / dpi, height / dpi)
canvas = FigureCanvasAgg(fig)
fig.savefig("out.png")
s, (w, h) = canvas.print_to_buffer()
print(w, h)

Actual outcome

100.0
956 927

Expected outcome

I expect 928.

Matplotlib version

  • Operating system: archlinux
  • Matplotlib version: 3.0.3
  • Matplotlib backend (print(matplotlib.get_backend())): not sure if applicable to my snippet
  • Python version: 3.7
  • Jupyter version (if applicable):
  • Other libraries:
    matplotlib was installed by pip install

Activity

  1. tacaswell commented on Oct 3, 2019

    @tacaswell
    Member

    The problem is that

    In [73]: int((928 / 100) * 100)
    Out[73]: 927
    
    In [75]: (928/ 100) * 100
    Out[75]: 927.9999999999999
    
    In [76]: (928/ 100) * 100 - 928
    Out[76]: -1.1368683772161603e-13
    

    because of the limited precision of floats and internally we are using truncation (rather than rounding) to go from inches -> pixels. I believe that this is the "next" float before 928.

    We had some discussion a while ago about changing from truncating to rounding (which is not unreasonable), however iirc, it broke a whole bunch of our tests (and is an API break because people who have been happy with the rounding down may suddenly get figures that are one pixel bigger). I think that conversation stalled out discussing the trade offs there.

    It is super hacky, but I think the most reliable thing to do for now is to bump up your target pixel size by a fraction of a pixel bigger, ex

    fig.set_size_inches((width + .1) / dpi, (height + .1) / dpi)

    to avoid this instability.

    Details

    This is a fun bit of detail:

    In [78]: [(n, (n / 100) * 100 - n) for n in range(900, 1000)]
    Out[78]: 
    [(900, 0.0),
     (901, 0.0),
     (902, 0.0),
     (903, -1.1368683772161603e-13),
     (904, -1.1368683772161603e-13),
     (905, 1.1368683772161603e-13),
     (906, 0.0),
     (907, 0.0),
     (908, 0.0),
     (909, 0.0),
     (910, 0.0),
     (911, 0.0),
     (912, -1.1368683772161603e-13),
     (913, 1.1368683772161603e-13),
     (914, 0.0),
     (915, 0.0),
     (916, 0.0),
     (917, 0.0),
     (918, 0.0),
     (919, 0.0),
     (920, -1.1368683772161603e-13),
     (921, 1.1368683772161603e-13),
     (922, 1.1368683772161603e-13),
     (923, 0.0),
     (924, 0.0),
     (925, 0.0),
     (926, 0.0),
     (927, 0.0),
     (928, -1.1368683772161603e-13),
     (929, -1.1368683772161603e-13),
     (930, 1.1368683772161603e-13),
     (931, 0.0),
     (932, 0.0),
     (933, 0.0),
     (934, 0.0),
     (935, 0.0),
     (936, 0.0),
     (937, -1.1368683772161603e-13),
     (938, 1.1368683772161603e-13),
     (939, 0.0),
     (940, 0.0),
     (941, 0.0),
     (942, 0.0),
     (943, 0.0),
     (944, 0.0),
     (945, -1.1368683772161603e-13),
     (946, 1.1368683772161603e-13),
     (947, 1.1368683772161603e-13),
     (948, 0.0),
     (949, 0.0),
     (950, 0.0),
     (951, 0.0),
     (952, 0.0),
     (953, -1.1368683772161603e-13),
     (954, -1.1368683772161603e-13),
     (955, 1.1368683772161603e-13),
     (956, 0.0),
     (957, 0.0),
     (958, 0.0),
     (959, 0.0),
     (960, 0.0),
     (961, 0.0),
     (962, -1.1368683772161603e-13),
     (963, 1.1368683772161603e-13),
     (964, 0.0),
     (965, 0.0),
     (966, 0.0),
     (967, 0.0),
     (968, 0.0),
     (969, 0.0),
     (970, -1.1368683772161603e-13),
     (971, 1.1368683772161603e-13),
     (972, 1.1368683772161603e-13),
     (973, 0.0),
     (974, 0.0),
     (975, 0.0),
     (976, 0.0),
     (977, 0.0),
     (978, -1.1368683772161603e-13),
     (979, -1.1368683772161603e-13),
     (980, 1.1368683772161603e-13),
     (981, 0.0),
     (982, 0.0),
     (983, 0.0),
     (984, 0.0),
     (985, 0.0),
     (986, 0.0),
     (987, -1.1368683772161603e-13),
     (988, 1.1368683772161603e-13),
     (989, 0.0),
     (990, 0.0),
     (991, 0.0),
     (992, 0.0),
     (993, 0.0),
     (994, 0.0),
     (995, -1.1368683772161603e-13),
     (996, 1.1368683772161603e-13),
     (997, 1.1368683772161603e-13),
     (998, 0.0),
     (999, 0.0)]
    
  2. ppwwyyxx commented on Oct 3, 2019

    @ppwwyyxx
    ContributorAuthor

    Thanks for sharing these details. Adding a small number seems like a nice hack to get around the truncation issue!

  3. timhoffm commented on Oct 3, 2019

    @timhoffm
    Member

    As mentioned above, this has been discussed before in #8265. I think the current state is just confusing and not at all what one would expect.

    Consequences of a possible change (when playing with #8265):

    • Some figures may change their size by 1px. This happens for two figures in the entire test suite.
    • Subpixel rendering may depend on this as well. Further 3 figures in tests slightly change the position of grids or text.

    Given that this affects only 5 of the images in our large test suite (*), I think the change is bearable.
    Also, I suspect there are very little people that really care about the exact pixel size and at the same time have accepted (and expect) their figure to be one pixel smaller due to the truncating.

    (*) This may be biased because we tend to often use the same image size and thus not sample the space equally. However, assuming that people tend to use multiples of 10 as figure size in pixels [(n, (n / 100) * 100 - n) for n in range(100, 1500, 10)] shows that 12% of sizes between 100px and 1500px are affected by the change. None of them are probably even more commonly used multiples of 50px.

  4. github-actions commented on Jun 28, 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!

  5. added
    status: inactive
    on Jun 28, 2023
  6. added
    status: closed as inactive
    on Jul 31, 2023
  7. added
    keep
    and removed
    status: inactive
    status: closed as inactive
    on Jul 14, 2025
  8. jklymak commented on Jul 14, 2025

    @jklymak
    Member

    I guess if we are changing many images for font changes, this would also be a good time to change this to rounding rather than truncation. Truncating 927.99999999983 to 927 is suboptimal. Of course there will be an instability around 0.5, but the folks who care about exactly how many pixels their figures are will hopefully not be hitting half pixel intervals.

  9. added this to the v3.11.1 milestone on Jul 16, 2026
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

    keep

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions