Matplotlib / PyQT4: 透明图形

3
我正在使用Python和PyQt4,想要在GUI中嵌入一个matplotlib图形。对我来说,重要的是Figure的背景与GUI的背景颜色相匹配(实际绘图周围没有灰色背景)。
我的部分解决方案是使用以下代码使Figure透明:
...
fig.patch.set_alpha(0.5)
...

当在pylab模式下绘图时,这个方法可以正常工作,但是在PyQt4中嵌入时,重新渲染不会清除旧图形,而是将其添加在顶部并给出透明度。如下面的示例所示,这是调整窗口大小的结果:

no working sample

样本是使用matplotlib网站上的代码生成的,并在MyMplCanvas__init__语句中添加了set_alpha(0.5)行。

最好将您使用的所有代码都粘贴在此处,而不仅仅提供链接。其他页面可能会更改,这将使您的问题不明确。 - tacaswell
请仅提供最少量的代码以展示您的问题。 - tacaswell
谢谢你的提示。你说得对,要简化问题并直接在SO上发布。只是在这种情况下,我想展示这个问题即使使用官方配方也会出现,而不仅仅是我的可能错误的方法。 - thengineer
在这种情况下,我认为正确的做法是发布官方代码,仅保留动态图部分。 - tacaswell
1个回答

1
你发现了Matplotlib中一个有趣的Bug。Qt4后端在重绘之前没有清除它用于显示图形的qImage,这就是为什么你看到了阴影的原因。通过向matplotlib.backends.backed_qt4Agg.FigureCanvasQTAgg.paintEvent添加几行代码可以轻松解决这个问题。
def paintEvent(self, e):
    """
    Copy the image from the Agg canvas to the qt.drawable.
    In Qt, all drawing should be done inside of here when a widget is
    shown onscreen.
    """

    #FigureCanvasQT.paintEvent(self, e)
    if DEBUG:
        print('FigureCanvasQtAgg.paintEvent: ', self,
            self.get_width_height())

    if self.blitbox is None:
        # matplotlib is in rgba byte order.  QImage wants to put the bytes
        # into argb format and is in a 4 byte unsigned int.  Little endian
        # system is LSB first and expects the bytes in reverse order
        # (bgra).
        if QtCore.QSysInfo.ByteOrder == QtCore.QSysInfo.LittleEndian:
            stringBuffer = self.renderer._renderer.tostring_bgra()
        else:
            stringBuffer = self.renderer._renderer.tostring_argb()

        refcnt = sys.getrefcount(stringBuffer)

        qImage = QtGui.QImage(stringBuffer, self.renderer.width,
                              self.renderer.height,
                              QtGui.QImage.Format_ARGB32)
        rect = qImage.rect()    ### <-- added this line
        p = QtGui.QPainter(self)
        p.eraseRect(rect)       ### <-- added this line
        p.drawPixmap(QtCore.QPoint(0, 0), QtGui.QPixmap.fromImage(qImage))

        # draw the zoom rectangle to the QPainter
        if self.drawRect:
            p.setPen(QtGui.QPen(QtCore.Qt.black, 1, QtCore.Qt.DotLine))
            p.drawRect(self.rect[0], self.rect[1],
                       self.rect[2], self.rect[3])
        p.end()

        # This works around a bug in PySide 1.1.2 on Python 3.x,
        # where the reference count of stringBuffer is incremented
        # but never decremented by QImage.
        # TODO: revert PR #1323 once the issue is fixed in PySide.
        del qImage
        if refcnt != sys.getrefcount(stringBuffer):
            _decref(stringBuffer)
    else:
        bbox = self.blitbox
        l, b, r, t = bbox.extents
        w = int(r) - int(l)
        h = int(t) - int(b)
        t = int(b) + h
        reg = self.copy_from_bbox(bbox)
        stringBuffer = reg.to_string_argb()
        qImage = QtGui.QImage(stringBuffer, w, h,
                              QtGui.QImage.Format_ARGB32)
        pixmap = QtGui.QPixmap.fromImage(qImage)
        p = QtGui.QPainter(self)
        p.drawPixmap(QtCore.QPoint(l, self.renderer.height-t), pixmap)
        p.end()
        self.blitbox = None
    self.drawRect = False

以diff形式的更改:

diff --git a/lib/matplotlib/backends/backend_qt4agg.py b/lib/matplotlib/backends/backend_qt4agg.py
index 8433731..718d352 100644
--- a/lib/matplotlib/backends/backend_qt4agg.py
+++ b/lib/matplotlib/backends/backend_qt4agg.py
@@ -118,7 +118,9 @@ class FigureCanvasQTAgg(FigureCanvasQT, FigureCanvasAgg):
             qImage = QtGui.QImage(stringBuffer, self.renderer.width,
                                   self.renderer.height,
                                   QtGui.QImage.Format_ARGB32)
+            rect = qImage.rect()
             p = QtGui.QPainter(self)
+            p.eraseRect(rect)
             p.drawPixmap(QtCore.QPoint(0, 0), QtGui.QPixmap.fromImage(qImage))

             # draw the zoom rectangle to the QPainter

你可以修改本地安装或在代码中进行猴子补丁。 PR #2449已合并,将包含在1.3.1版本中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接