Skip to content

[Bug]: BufferRegion get_extents does not return negative values #23491

Open
@hugochrist1

Description

@hugochrist1

Bug summary

Calling get_extents on a BufferRegion returns the values as unsigned ints so if the region was outside of the canvas, they are incorrect.
The y-axis increases downwards.

Code for reproduction

import matplotlib.pyplot as plt
from matplotlib.transforms import Bbox
import struct

fig = plt.figure()
canvas = fig.canvas
w,h = canvas.get_width_height()
br = canvas.copy_from_bbox(Bbox.from_extents(-10,h-1,10,h+1))
ext = br.get_extents()
print(ext,struct.unpack('iiii',struct.pack('IIII',*ext)))

Actual outcome

(4294967286, 4294967295, 10, 1) (-10, -1, 10, 1)

Expected outcome

(-10, -1, 10, 1)

Additional information

I think the problem comes from this:
https://github.com/matplotlib/matplotlib/blob/main/src/_backend_agg_wrapper.cpp#L77

static PyObject *PyBufferRegion_get_extents(PyBufferRegion *self, PyObject *args)
{
    agg::rect_i rect = self->x->get_rect();

    return Py_BuildValue("IIII", rect.x1, rect.y1, rect.x2, rect.y2);
}

Replacing 'IIII' with 'iiii' would solve it

Operating system

Windows 10

Matplotlib Version

3.5.2

Matplotlib Backend

TkAgg

Python version

3.10.4

Jupyter version

No response

Installation

pip

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