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

Code before:
        # docstring inherited
        return super().ignore(event) or self.canvas is None

    def _set_cursor(self, cursor):
        """Update the canvas cursor."""
        self.ax.get_figure(root=True).canvas.set_cursor(cursor)

    def _save_blit_background(self, background):
        """
        Save a blit background.

        The background is stored on the canvas in a uniquely identifiable way.
        It should be read back via `._load_blit_background`. Be prepared that
        some events may invalidate the background, in which case
        `._load_blit_background` will return None.

        This currently allows at most one background per widget, which is
        good enough for all existing widgets.
        """
        if self._blit_background_id is None:
            self._blit_background_id = self.canvas._get_blit_background_id()
        self.canvas._blit_backgrounds[self._blit_background_id] = background

    def _load_blit_background(self):
        """Load a blit background; may be None at any time."""
        return self.canvas._blit_backgrounds.get(self._blit_background_id)


class Button(AxesWidget):
    """
    A GUI neutral button.

    For the button to remain responsive you must keep a reference to it.
    Call `.on_clicked` to connect to the button.

    Attributes
    ----------
    ax
        The `~.axes.Axes` the button renders into.
    label
        A `.Text` instance.
    color
        The color of the button when not hovering.
    hovercolor
        The color of the button when hovering.
    """

    def __init__(self, ax, label, image=None,
                 color='0.85', hovercolor='0.95', *, useblit=True):
        """
        Parameters

Review comment (performance):
A second thought:

In practice we only have one background per widget, but because the current API is scoped to a bounding box a widget _could_ have more than one.  Do we want to deal with that complexity here or kick that down the road?  This is coupled to Antony's suggestion to standardized the return type. 

Code after:
        return ((event.xdata, event.ydata) if event.inaxes is self.ax
                else self.ax.transData.inverted().transform((event.x, event.y)))

    def ignore(self, event):
        # docstring inherited
        return super().ignore(event) or self.canvas is None

    def _set_cursor(self, cursor):
        """Update the canvas cursor."""
        self.ax.get_figure(root=True).canvas.set_cursor(cursor)

    def _save_blit_background(self, background):
        """
        Save a blit background.

        The background is stored on the canvas in a uniquely identifiable way.
        It should be read back via `._load_blit_background`. Be prepared that
        some events may invalidate the background, in which case
        `._load_blit_background` will return None.

        This currently allows at most one background per widget, which is
        good enough for all existing widgets.
        """
        if self._blit_background_id is None:
            self._blit_background_id = self.canvas._get_blit_background_id()
        self.canvas._blit_backgrounds[self._blit_background_id] = background

    def _load_blit_background(self):
        """Load a blit background; may be None at any time."""
        return self.canvas._blit_backgrounds.get(self._blit_background_id)


class Button(AxesWidget):
    """
    A GUI neutral button.

    For the button to remain responsive you must keep a reference to it.
    Call `.on_clicked` to connect to the button.

    Attributes
    ----------
    ax
        The `~.axes.Axes` the button renders into.
    label
        A `.Text` instance.
    color
        The color of the button when not hovering.
    hovercolor
        The color of the button when hovering.
    """