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
ValueError: cannot reshape array of size 13109760 into shape (1708,2560,3)
Expected outcome
array of size (1708,2560,3)
Additional information
Problem appears from rounding error in variable "height_inches" which results in wrong argument in line.
At this line width is equal to 1707.9999999999998 and int(1707.9999999999998)=1707 but not 1708 as expected
I can produce this with on ~main and a duplicate of #15363
#8265 is a pass at moving to round rather than int, but it is now far out of date (and was failing tests due to the changes induced) and I suspect does not yet hit enough places (a number of the GUI frameworks just got stricter about ints vs floats so we now have more explicit casts than we used to).
@blatr@QuLogic@ksunden Hi, I am a student at CMU and for an end of the year project, my team has to pick an issue on matplotlib and try to fix it. We chose this issue. We were able to fix the example code above and get it to run without an error. We were wondering if this is a user error issue or a bug in the matplotlib code? Here is how we fixed the code for reproduction below:
Is the fundamental problem (which is not even on MPL, directly).
One fix, as mentioned previously, is to use round instead of int for these conversions... that has some knock-on effects that some test images may be changed which is annoying, but workable probably.
I think another fix would be to have a set_size_pixels method that allows you to pass the int values in (and probably uses the dpi to compute the size in inches, since aspect ratio can change there) but have that be guaranteed to produce figures of that size (may be tricky though, as internally I think we use inches for a lot, and so not sure it'd be trivial to make the guarantee).
See also #1369#9226 (these are mostly about using metric units instead of inches, but pixels is another unit that might make sense)
Activity
tacaswell commentedon Dec 7, 2021
I can produce this with on ~main and a duplicate of #15363
#8265 is a pass at moving to round rather than
int, but it is now far out of date (and was failing tests due to the changes induced) and I suspect does not yet hit enough places (a number of the GUI frameworks just got stricter about ints vs floats so we now have more explicit casts than we used to).TangChiaHsin commentedon Dec 12, 2021
Adding .1 can not solve this problem in macOS system.
The figure size will be changed into 4266404.
oliviavanzandt commentedon Nov 28, 2023
@blatr Hi, is this still an issue or has it been resolved?
oliviavanzandt commentedon Dec 4, 2023
@blatr @QuLogic @ksunden Hi, I am a student at CMU and for an end of the year project, my team has to pick an issue on matplotlib and try to fix it. We chose this issue. We were able to fix the example code above and get it to run without an error. We were wondering if this is a user error issue or a bug in the matplotlib code? Here is how we fixed the code for reproduction below:
ksunden commentedon Dec 4, 2023
This is a problem in matplotlib (well, moreso just a problem in using floats for something that is then discretized)
We are not interested in modifying the resultant image after the fact here
Is the fundamental problem (which is not even on MPL, directly).
One fix, as mentioned previously, is to use
roundinstead ofintfor these conversions... that has some knock-on effects that some test images may be changed which is annoying, but workable probably.I think another fix would be to have a
set_size_pixelsmethod that allows you to pass the int values in (and probably uses the dpi to compute the size in inches, since aspect ratio can change there) but have that be guaranteed to produce figures of that size (may be tricky though, as internally I think we use inches for a lot, and so not sure it'd be trivial to make the guarantee).See also #1369 #9226 (these are mostly about using metric units instead of inches, but pixels is another unit that might make sense)