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

Code before:
            elif halign == 'right':
                offsetx = xmax1
            else:
                offsetx = xmin1

            if valign == 'center':
                offsety = (ymin1 + ymax1) / 2.0
            elif valign == 'top':
                offsety = ymax1
            elif valign == 'baseline':
                offsety = ymax1 - baseline
            elif valign == 'center_baseline':
                offsety = ymax1 - baseline / 2.0
            else:
                offsety = ymin1

            if rotation != 0:
                offsetx, offsety = M.transform((offsetx, offsety))

        xmin -= offsetx
        ymin -= offsety

        bbox = Bbox.from_bounds(xmin, ymin, width, height)

        # now rotate the positions around the first (x, y) position
        if rotation != 0:
            xys = (M.transform(offset_layout) - (offsetx, offsety)).tolist()
        else:
            xys = [(x - offsetx, y - offsety) for x, y in offset_layout]

        info = [(ln, (w, h), xy[0], xy[1])
                for ln, w, h, xy in zip(lines, ws, hs, xys)]
        return bbox, info, descent

    def set_bbox(self, rectprops):
        """
        Draw a box behind/around the text.

        This can be used to set a background and/or a frame around the text.
        It's realized through a `.FancyBboxPatch` behind the text (see also
        `.Text.get_bbox_patch`). The bbox patch is None by default and only
        created when needed.

        Parameters
        ----------
        rectprops : dict with properties for `.FancyBboxPatch` or None
             The default boxstyle is 'square'. The mutation
             scale of the `.patches.FancyBboxPatch` is set to the fontsize.

             Pass ``None`` to remove the bbox patch completely.


Review comment (suggestion):
If we're going for this kind of microoptimizations, I would strongly suspect that it may be faster to convert everything to python scalars (not numpy scalars) and completely avoid the use of numpy here (writing out explicitly the rotation operations and so on), because we're dealing in general with very few characters ("no one" is writing thousands of characters with matplotlib) and numpy's overhead is actually large in this case.

Code after:
        height = ymax - ymin

        # Now move the box to the target position offset the display
        # bbox by alignment
        halign = self._horizontalalignment
        valign = self._verticalalignment

        rotation_mode = self.get_rotation_mode()
        if rotation_mode != "anchor":
            if rotation_mode == 'xtick':
                halign = self._ha_for_angle(angle)
            elif rotation_mode == 'ytick':
                valign = self._va_for_angle(angle)
            # compute the text location in display coords and the offsets
            # necessary to align the bbox with that location
            if halign == 'center':
                offsetx = (xmin + xmax) / 2
            elif halign == 'right':
                offsetx = xmax
            else:
                offsetx = xmin

            if valign == 'center':
                offsety = (ymin + ymax) / 2
            elif valign == 'top':
                offsety = ymax
            elif valign == 'baseline':
                offsety = ymin + descent
            elif valign == 'center_baseline':
                offsety = ymin + height - baseline / 2.0
            else:
                offsety = ymin
        else:
            xmin1, ymin1 = corners_horiz[0]
            xmax1, ymax1 = corners_horiz[2]

            if halign == 'center':
                offsetx = (xmin1 + xmax1) / 2.0
            elif halign == 'right':
                offsetx = xmax1
            else:
                offsetx = xmin1

            if valign == 'center':
                offsety = (ymin1 + ymax1) / 2.0
            elif valign == 'top':
                offsety = ymax1
            elif valign == 'baseline':
                offsety = ymax1 - baseline
            elif valign == 'center_baseline':
                offsety = ymax1 - baseline / 2.0