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 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.
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
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.
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 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.
Activity
tacaswell commentedon Oct 3, 2019
The problem is that
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
to avoid this instability.
Details
This is a fun bit of detail:
ppwwyyxx commentedon Oct 3, 2019
Thanks for sharing these details. Adding a small number seems like a nice hack to get around the truncation issue!
timhoffm commentedon Oct 3, 2019
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):
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.github-actions commentedon Jun 28, 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!
jklymak commentedon Jul 14, 2025
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.