gpt4 book ai didi

matplotlib 和透明度图

转载 作者:行者123 更新时间:2023-12-02 00:12:59 27 4
gpt4 key购买 nike

我正在使用 matplotlib 库和 PyQt5Python 3.6 。我在我创建的窗口中添加了一个图形,并且我希望将这个图形的背景设置为透明,因为我向窗口的背景添加了一个图像。但是,该图形并不是真正透明,它复制了窗口的背景图像。
例如,有人在两年前处理同样的问题:
matplotlib and pyqt4 transparent background

这是一个工作示例(背景为黑色但图形不是黑色):

import sys, os
from PyQt5.QtCore import Qt
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

import matplotlib
matplotlib.use('Qt5Agg') # Make sure that we are using QT5
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
import matplotlib.pyplot as plt

class SecondWindow(QWidget):
def __init__(self, parent=None):
super(SecondWindow, self).__init__(parent)
self.setupUi(self)

def setupUi(self, Form):
# WINDOW SETTINGS
Form.setWindowTitle('Hello')
self.p = QPalette()
self.pixmap = QPixmap(os.getcwd() + "/logo.png").scaled(self.size(), Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
self.p.setBrush(QPalette.Background, QBrush(self.pixmap))
self.setPalette(self.p)

# CREATE FIGURE AND SETTINGS
self.figure = plt.figure()
self.figure.patch.set_facecolor('None')
self.figure.patch.set_alpha(0)
self.canvas = FigureCanvas(self.figure)
self.axes = self.figure.add_subplot(111)

# WINDOW LAYOUT (with H1 and H2)
self.setLayout(QVBoxLayout())
self.layout().addWidget(self.canvas,1)
self.layout().setContentsMargins(50, 50, 50, 50)

if __name__ == '__main__':
app = QApplication(sys.argv)
form = SecondWindow()
form.show()
sys.exit(app.exec_())

我在长时间内寻找答案,但还没有找到解决方案。感谢您提供的任何帮助!
  • 操作系统:Windows 7 Pro
  • Matplotlib 版本:2.0.2(通过 Anaconda 安装,conda install matplotlib --channel conda-forge)
  • Python 版本:Python 3.6
  • python 3
  • 最佳答案

    出现问题是因为背景图像被设置为小部件的调色板。这会导致 Canvas 继承调色板,因此 Canvas 也会将图像作为背景,以某种方式覆盖小部件的背景。

    一个解决方案是将 Canvas 的背景设置为透明。一个简单的方法是样式表。

    self.canvas.setStyleSheet("background-color:transparent;")

    请注意,这与将补丁的 facecolor 设置为 none 不同。该图形有一个背景,它在 matplotlib 中控制,但作为 PyQt 对象的 Canvas 也有一个背景。

    完整示例:
    import sys, os
    from PyQt4.QtCore import Qt
    from PyQt4.QtGui import *

    import matplotlib
    matplotlib.use('Qt4Agg')
    from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
    from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
    from matplotlib.figure import Figure
    import matplotlib.pyplot as plt
    plt.rcParams['xtick.color'] ="w"
    plt.rcParams['ytick.color'] ="w"
    plt.rcParams['font.size'] = 14


    class SecondWindow(QWidget):
    def __init__(self, parent=None):
    super(SecondWindow, self).__init__(parent)

    # CREATE FIGURE AND SETTINGS
    self.figure = plt.figure()
    self.figure.patch.set_facecolor("None")
    self.canvas = FigureCanvas(self.figure)
    self.axes = self.figure.add_subplot(111)
    self.axes.patch.set_alpha(0.5)

    ###### Make the background of the canvas transparent
    self.canvas.setStyleSheet("background-color:transparent;")

    self.p = QPalette()
    self.p.setBrush(QPalette.Background, QBrush(QPixmap("house.png")))
    self.setPalette(self.p)

    self.setLayout(QVBoxLayout())
    self.layout().addWidget(self.canvas,1)
    self.layout().setContentsMargins(50, 50, 50, 50)

    if __name__ == '__main__':
    app = QApplication(sys.argv)
    form = SecondWindow()
    form.show()
    sys.exit(app.exec_())

    这可能看起来像

    enter image description here

    关于matplotlib 和透明度图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45631428/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com