Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Matplotlib/PyQT4:透明图形_Python_Matplotlib_Pyqt4 - Fatal编程技术网

Python Matplotlib/PyQT4:透明图形

Python Matplotlib/PyQT4:透明图形,python,matplotlib,pyqt4,Python,Matplotlib,Pyqt4,我将Python与PyQt4一起使用,我希望在GUI中嵌入matplotlib图形。 对我来说重要的是,图的背景与GUI的背景颜色相匹配。(实际地块周围无灰色背景) 我的部分解决方案是使用以下代码使图透明: ... fig.patch.set_alpha(0.5) ... 当在pylab模式下打印时,这很好,但是当嵌入PyQt4时,重新渲染不会清除旧图形,而是在顶部添加我提供的透明度。如下面的示例所示,这是调整窗口大小的结果: 样本是通过在mymplnavas的\uuuu init\uuuu

我将Python与PyQt4一起使用,我希望在GUI中嵌入matplotlib图形。 对我来说重要的是,
图的背景与GUI的背景颜色相匹配。(实际地块周围无灰色背景)

我的部分解决方案是使用以下代码使
透明:

...
fig.patch.set_alpha(0.5)
...
当在pylab模式下打印时,这很好,但是当嵌入PyQt4时,重新渲染不会清除旧图形,而是在顶部添加我提供的透明度。如下面的示例所示,这是调整窗口大小的结果:


样本是通过在
mymplnavas
\uuuu init\uuuu
语句中添加
set\u alpha(0.5)
行生成的

您在
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
您可以修改本地安装,也可以在代码中对其进行修补


它已被合并并将出现在1.3.1中

最好粘贴您在此处使用的所有代码,而不是仅提供链接。其他页面可能会更改,这将使您的问题不适定。还可以将其缩减到显示您的问题所需的最小值。谢谢您的提示。关于减少问题并将其直接发布到SO上,您是对的。就在这种情况下,我想表明,即使是官方配方也会出现问题,而不仅仅是我的潜在错误方式。在这种情况下,我认为正确的做法是将官方代码剥离到动态图部分。
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