Skip to content

Fixing Issue #21879: Changed int to round to help with floating point issue #27473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions lib/matplotlib/backends/backend_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def __init__(self, width, height, dpi):
self.dpi = dpi
self.width = width
self.height = height
self._renderer = _RendererAgg(int(width), int(height), dpi)
self._renderer = _RendererAgg(int(round(width)), int(round(height)), dpi)
self._filter_renderers = []

self._update_methods()
Expand Down Expand Up @@ -99,7 +99,7 @@ def draw_path(self, gc, path, transform, rgbFace=None):
if (npts > nmax > 100 and path.should_simplify and
rgbFace is None and gc.get_hatch() is None):
nch = np.ceil(npts / nmax)
chsize = int(np.ceil(npts / nch))
chsize = int(round((npts / nch)))
i0 = np.arange(0, npts, chsize)
i1 = np.zeros_like(i0)
i1[:-1] = i0[1:] - 1
Expand Down Expand Up @@ -179,8 +179,8 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):

xd = descent * sin(radians(angle))
yd = descent * cos(radians(angle))
x = round(x + ox + xd)
y = round(y - oy + yd)
x = int(round(x + ox + xd))
y = int(round(y - oy + yd))
self._renderer.draw_text_image(font_image, x, y + 1, angle, gc)

def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
Expand All @@ -200,8 +200,8 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
yo /= 64.0
xd = d * sin(radians(angle))
yd = d * cos(radians(angle))
x = round(x + xo + xd)
y = round(y + yo + yd)
x = int(round(x + xo + xd))
y = int(round(y + yo + yd))
self._renderer.draw_text_image(font, x, y + 1, angle, gc)

def get_text_width_height_descent(self, s, prop, ismath):
Expand Down Expand Up @@ -238,8 +238,8 @@ def draw_tex(self, gc, x, y, s, prop, angle, *, mtext=None):
w, h, d = self.get_text_width_height_descent(s, prop, ismath="TeX")
xd = d * sin(radians(angle))
yd = d * cos(radians(angle))
x = round(x + xd)
y = round(y + yd)
x = int(round(x + xd))
y = int(round(y + yd))
self._renderer.draw_text_image(Z, x, y, angle, gc)

def get_canvas_width_height(self):
Expand Down Expand Up @@ -314,8 +314,9 @@ def restore_region(self, region, bbox=None, xy=None):

# The incoming data is float, but the _renderer type-checking wants
# to see integers.
self._renderer.restore_region(region, int(x1), int(y1),
int(x2), int(y2), int(ox), int(oy))
self._renderer.restore_region(region, int(round(x1)), int(round(y1)),
int(round(x2)), int(round(y2)),
int(round(ox)), int(round(oy)))

else:
self._renderer.restore_region(region)
Expand All @@ -325,7 +326,7 @@ def start_filter(self):
Start filtering. It simply creates a new canvas (the old one is saved).
"""
self._filter_renderers.append(self._renderer)
self._renderer = _RendererAgg(int(self.width), int(self.height),
self._renderer = _RendererAgg(int(round(self.width)), int(round(self.height)),
self.dpi)
self._update_methods()

Expand Down Expand Up @@ -360,7 +361,7 @@ def post_processing(image, dpi):
if img.dtype.kind == 'f':
img = np.asarray(img * 255., np.uint8)
self._renderer.draw_image(
gc, slice_x.start + ox, int(self.height) - slice_y.stop + oy,
gc, slice_x.start + ox, int(round(self.height)) - slice_y.stop + oy,
img[::-1])


Expand Down Expand Up @@ -499,7 +500,7 @@ def print_to_buffer(self):
FigureCanvasAgg.draw(self)
renderer = self.get_renderer()
return (bytes(renderer.buffer_rgba()),
(int(renderer.width), int(renderer.height)))
(int(round(renderer.width)), int(round(renderer.height))))

# Note that these methods should typically be called via savefig() and
# print_figure(), and the latter ensures that `self.figure.dpi` already
Expand Down
10 changes: 10 additions & 0 deletions lib/matplotlib/tests/test_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,13 @@ def test_non_tuple_rgbaface():
fig.add_subplot(projection="3d").scatter(
[0, 1, 2], [0, 1, 2], path_effects=[patheffects.Stroke(linewidth=4)])
fig.canvas.draw()


def test_round_function():
dpi = 100
h = 1708
w = 2560
ra = RendererAgg(w, h, dpi)
ra2 = ra._renderer()
assert ra2.w == w
assert ra2.h == h