8000 [Bug]: bbox floating point size mismatch · Issue #21879 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
[Bug]: bbox floating point size mismatch #21879
Open
@blatr

Description

@blatr

Bug summary

Hi! matplotlib treats incorrectly floating figsize.

Code for reproduction

import matplotlib.pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
import numpy as np

dpi = 100
h = 1708
w = 2560
height_inches = float(h) / dpi
width_inches = float(w) / dpi
fig = plt.figure(figsize=(width_inches, height_inches), dpi=dpi)
plt.axes([0, 0, 1, 1])
plt.axis("off")

canvas = FigureCanvas(fig)
extent = (0, 2560, 1708, 0)
plt.imshow(np.zeros((2560, 1708,3)), extent=extent)
canvas.draw()
fig.clear()
plt.close(fig)

original_image = np.fromstring(canvas.tostring_rgb(), dtype="uint8").reshape(h, w, 3)

Actual outcome

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

Operating system

Ubuntu 18.04.5 LTS (Bionic Beaver)

Matplotlib Version

3.3.4

Matplotlib Backend

agg

Python version

3.7.10

Jupyter version

No response

Installation

conda

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0