Repository: matplotlib/matplotlib
File: lib/matplotlib/offsetbox.py (Python)

Code before:
            if l == 2:
                bbox = [bbox[0], bbox[1], 0, 0]

            self._bbox_to_anchor = Bbox.from_bounds(*bbox)

        self._bbox_to_anchor_transform = transform
        self.stale = True

    @_compat_get_offset

    def get_offset(self, bbox, renderer):
        fontsize_in_pixels = renderer.points_to_pixels(self.prop.get_size_in_points())
        try:
            borderpad_x, borderpad_y = self.borderpad
        except (TypeError, ValueError):
            borderpad_x = self.borderpad
            borderpad_y = self.borderpad
        pad_x_pixels = borderpad_x * fontsize_in_pixels
        pad_y_pixels = borderpad_y * fontsize_in_pixels
        bbox_to_anchor = self.get_bbox_to_anchor()
        padded_bbox_to_anchor = Bbox.from_extents(
            bbox_to_anchor.x0 + pad_x_pixels,
            bbox_to_anchor.y0 + pad_y_pixels,
            bbox_to_anchor.x1 - pad_x_pixels,
            bbox_to_anchor.y1 - pad_y_pixels
        )
        x0, y0 = _get_anchored_bbox(
            self.loc,
            Bbox.from_bounds(0, 0, bbox.width, bbox.height),
            padded_bbox_to_anchor,
            0
        )
        return x0 - bbox.x0, y0 - bbox.y0

    def update_frame(self, bbox, fontsize=None):
        self.patch.set_bounds(bbox.bounds)
        if fontsize:
            self.patch.set_mutation_scale(fontsize)

    def draw(self, renderer):
        # docstring inherited
        if not self.get_visible():
            return

        # update the location and size of the legend
        bbox = self.get_window_extent(renderer)
        fontsize = renderer.points_to_pixels(self.prop.get_size_in_points())
        self.update_frame(bbox, fontsize)
        self.patch.draw(renderer)

        px, py = self.get_offset(self.get_bbox(renderer), renderer)

Review comment (suggestion):
Could we instead modify `_get_anchored_bbox` to accept two pad values?  Note that the [BBox.padded](https://matplotlib.org/stable/api/transformations.html#matplotlib.transforms.BboxBase.padded) method will accept different values for x and y.  I think we can similarly make the y value optional in `_get_anchored_bbox`.

Code after:
            except TypeError as err:
                raise ValueError(f"Invalid bbox: {bbox}") from err

            if l == 2:
                bbox = [bbox[0], bbox[1], 0, 0]

            self._bbox_to_anchor = Bbox.from_bounds(*bbox)

        self._bbox_to_anchor_transform = transform
        self.stale = True

    @_compat_get_offset
    def get_offset(self, bbox, renderer):
        # docstring inherited
        fontsize_in_pixels = renderer.points_to_pixels(self.prop.get_size_in_points())
        try:
            borderpad_x, borderpad_y = self.borderpad
        except TypeError:
            borderpad_x = self.borderpad
            borderpad_y = self.borderpad
        pad_x_pixels = borderpad_x * fontsize_in_pixels
        pad_y_pixels = borderpad_y * fontsize_in_pixels
        bbox_to_anchor = self.get_bbox_to_anchor()
        x0, y0 = _get_anchored_bbox(
            self.loc,
            Bbox.from_bounds(0, 0, bbox.width, bbox.height),
            bbox_to_anchor,
            pad_x_pixels,
            pad_y_pixels
        )
        return x0 - bbox.x0, y0 - bbox.y0

    def update_frame(self, bbox, fontsize=None):
        self.patch.set_bounds(bbox.bounds)
        if fontsize:
            self.patch.set_mutation_scale(fontsize)

    def draw(self, renderer):
        # docstring inherited
        if not self.get_visible():
            return

        # update the location and size of the legend
        bbox = self.get_window_extent(renderer)
        fontsize = renderer.points_to_pixels(self.prop.get_size_in_points())
        self.update_frame(bbox, fontsize)
        self.patch.draw(renderer)

        px, py = self.get_offset(self.get_bbox(renderer), renderer)
        self.get_child().set_offset((px, py))
        self.get_child().draw(renderer)