Repository: matplotlib/matplotlib
File: lib/matplotlib/backends/backend_qt.py (Python)

Code before:
        if self.toolbar:
            dpr = self.toolbar.devicePixelRatioF() or 1
        else:
            dpr = 1

        # Try SVG first, then fall back to PNG
        svg_path = self.image_path.with_suffix('.svg')
        if svg_path.exists():
            return self._create_pixmap(svg_path, size, dpr, is_svg=True)
        else:
            large_path = self.image_path.with_name(self.image_path.stem + '_large.png')
            path = large_path if large_path.exists() else self.image_path
            return self._create_pixmap(path, size, dpr, is_svg=False)

    def _create_pixmap(self, image_path, size, dpr, is_svg=False):
        """Create a pixmap from image file with proper scaling and dark mode support."""
        # Create high-resolution pixmap
        scaled_size = QtCore.QSize(int(size.width() * dpr), int(size.height() * dpr))
        pixmap = QtGui.QPixmap(scaled_size)
        pixmap.setDevicePixelRatio(dpr)
        pixmap.fill(QtCore.Qt.GlobalColor.transparent)

        if is_svg:
            return self._render_svg(image_path, pixmap, size, dpr)
        else:
            return self._render_png(image_path, pixmap, scaled_size, dpr)

    def _render_svg(self, svg_path, pixmap, size, dpr):
        """Render SVG content to pixmap."""
        QSvgRenderer = qt_compat.get_qsvg_renderer()
        if QSvgRenderer is None:
            return QtGui.QPixmap()

        svg_content = svg_path.read_bytes()

        if self._is_dark_mode():
            svg_content = svg_content.replace(b'fill:black;', b'fill:white;')
            svg_content = svg_content.replace(b'stroke:black;', b'stroke:white;')

        renderer = QSvgRenderer(QtCore.QByteArray(svg_content))
        if not renderer.isValid():
            return QtGui.QPixmap()

        painter = QtGui.QPainter()
        try:
            painter.begin(pixmap)
            renderer.render(painter, QtCore.QRectF(0, 0, size.width(), size.height()))
        finally:
            if painter.isActive():
                painter.end()


Review comment (suggestion):
We can simpify this by merging `_create_pixmap`:

```suggestion
        scaled_size = QtCore.QSize(int(size.width() * dpr), int(size.height() * dpr))
        pixmap = QtGui.QPixmap(scaled_size)
        pixmap.setDevicePixelRatio(dpr)
        pixmap.fill(QtCore.Qt.GlobalColor.transparent)
        
        # Try SVG first, then fall back to PNG
        svg_path = self.image_path.with_suffix('.svg')
        if svg_path.exists():
            return self._render_svg(svg_path, pixmap, size, dpr)
        else:
            large_path = self.image_path.with_name(self.image_path.stem + '_large.png')
            path = large_path if large_path.exists() else self.image_path
            return self._render_png(path, pixmap, scaled_size, dpr)
```



Code after:
        svg_path = self.image_path.with_suffix('.svg')
        if svg_path.exists():
            pixmap = self._create_pixmap_from_svg(svg_path, size)
            if not pixmap.isNull():
                return pixmap
        return self._create_pixmap_from_png(self.image_path, size)

    def _devicePixelRatio(self):
        """Return the current device pixel ratio for the toolbar, defaulting to 1."""
        return (self.toolbar.devicePixelRatioF() or 1) if self.toolbar else 1

    def _create_pixmap_from_svg(self, svg_path, size):
        """Create a pixmap from SVG with proper scaling and dark mode support."""
        QSvgRenderer = getattr(QtSvg, "QSvgRenderer", None)
        if QSvgRenderer is None:
            return QtGui.QPixmap()

        svg_content = svg_path.read_bytes()

        if self._is_dark_mode():
            svg_content = svg_content.replace(b'fill:black;', b'fill:white;')
            svg_content = svg_content.replace(b'stroke:black;', b'stroke:white;')

        renderer = QSvgRenderer(QtCore.QByteArray(svg_content))
        if not renderer.isValid():
            return QtGui.QPixmap()

        dpr = self._devicePixelRatio()
        scaled_size = QtCore.QSize(int(size.width() * dpr), int(size.height() * dpr))
        pixmap = QtGui.QPixmap(scaled_size)
        pixmap.setDevicePixelRatio(dpr)
        pixmap.fill(QtCore.Qt.GlobalColor.transparent)

        painter = QtGui.QPainter()
        try:
            painter.begin(pixmap)
            renderer.render(painter, QtCore.QRectF(0, 0, size.width(), size.height()))
        finally:
            if painter.isActive():
                painter.end()

        return pixmap

    def _create_pixmap_from_png(self, base_path, size):
        """
        Create a pixmap from PNG with scaling and dark mode support.

        Prefer to use the *_large.png with the same name; otherwise, use base_path.
        """
        large_path = base_path.with_name(base_path.stem + '_large.png')
        source_pixmap = QtGui.QPixmap()