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

Code before:
        # Create text objects to measure widths.
        # We create Text objects directly rather than using ax.text() since we're
        # only measuring them and only later add them to the axes.
        self.labels = [
            mtext.Text(0, 0, text=label, transform=self.ax.transAxes,
                       horizontalalignment="left", verticalalignment="center",
                       **props)
            for label, props in zip(labels, label_props)
        ]
        # Set figure reference so Text objects can access figure properties
        for text in self.labels:
            text.set_figure(fig)
        # Calculate max text width per column (in axes coordinates)
        col_widths = [
            max(
                (
                    text.get_window_extent(
                        self.ax.figure.canvas.get_renderer(),
                    ).transformed(
                        fig.dpi_scale_trans.inverted()
                    ).width * 72
                    for text in self.labels[col_idx::n_cols]
                ),
                default=0,
            )
            / axes_width_display
            for col_idx in range(n_cols)
        ]

        # Center rows vertically in the axes
        ys_per_row = np.linspace(1, 0, n_rows + 2)[1:-1]
        # Calculate x positions based on text widths
        col_x_positions = [left_margin]  # First column starts at left margin
        for col_idx in range(n_cols - 1):
            col_x_positions.append(
                col_x_positions[-1] +
                button_text_offset +
                col_widths[col_idx] +
                col_spacing
            )
        label_idx = np.arange(n_labels)
        self._buttons_xs = np.take(col_x_positions, label_idx % n_cols)
        self._buttons_ys = ys_per_row[label_idx // n_cols]
        for text, x, y in zip(self.labels, self._buttons_xs, self._buttons_ys):
            text.set_position((x + button_text_offset, y))
            self.ax.add_artist(text)

    def _init_props(self, text_size, **kwargs):
        raise NotImplementedError("This method should be defined in subclasses")

    def _clear(self, event):

Review comment (refactor):
If black is making you do this, it means you need to refactor.
```suggestion
        renderer = self.ax.figure.canvas.get_renderer()
        inv_trans = fig.dpi_scale_trans.inverted()
        col_widths = [
            max(
                (
                    text.get_window_extent(renderer).transformed(inv_trans).width * 72
                    for text in self.labels[col_idx::n_cols]
                ),
                default=0,
            )
            / axes_width_display
            for col_idx in range(n_cols)
        ]
```

Code after:
        # Create text objects to measure widths.
        # We create Text objects directly rather than using ax.text() since we're
        # only measuring them and only later add them to the axes.
        self.labels = [
            mtext.Text(0, 0, text=label, transform=self.ax.transAxes,
                       horizontalalignment="left", verticalalignment="center",
                       **props)
            for label, props in zip(labels, label_props)
        ]
        # Set figure reference so Text objects can access figure properties
        for text in self.labels:
            text.set_figure(fig)
        # Calculate max text width per column (in axes coordinates)
        renderer = self.ax.figure.canvas.get_renderer()
        inv_trans = fig.dpi_scale_trans.inverted()
        col_widths = [
            max(
                (
                    text.get_window_extent(renderer).transformed(inv_trans).width * 72
                    for text in self.labels[col_idx::n_cols]
                ),
                default=0,
            )
            / axes_width_display
            for col_idx in range(n_cols)
        ]

        # Center rows vertically in the axes
        ys_per_row = np.linspace(1, 0, n_rows + 2)[1:-1]
        # Calculate x positions based on text widths
        col_x_positions = [left_margin]  # First column starts at left margin
        for col_idx in range(n_cols - 1):
            col_x_positions.append(
                col_x_positions[-1] +
                button_text_offset +
                col_widths[col_idx] +
                col_spacing
            )
        label_idx = np.arange(n_labels)
        self._buttons_xs = np.take(col_x_positions, label_idx % n_cols)
        self._buttons_ys = ys_per_row[label_idx // n_cols]
        for text, x, y in zip(self.labels, self._buttons_xs, self._buttons_ys):
            text.set_position((x + button_text_offset, y))
            self.ax.add_artist(text)

    def _init_props(self, text_size, **kwargs):
        raise NotImplementedError("This method should be defined in subclasses")

    def _clear(self, event):
        """Internal event handler to clear the buttons."""
        if self.ignore(event) or self.canvas.is_saving():