Matplotlib:如何使背景在Windows上透明

问题描述 投票:0回答:1

我需要整个绘图窗口都是透明的,以便可以通过绘图看到例如桌面上的chrome窗口,以便我可以在其上添加点,同时查看其背后的内容。

https://stackoverflow.com/a/45505906/13650485

我上面列出的答案恰好是我想要做的,除了我的交互式系统不适用于TK。我想使用Qt5Agg。当我运行上面的代码时,系统将不接受它-它说QT5当​​前正在运行。如果我在未加载QT的情况下运行它,它将创建一个空白的透明窗口(是的!),但是如果我移动它或单击该图标,它将变为不透明的黑色,而没有任何绘制。如果我将tk更改为Qt5,它将抱怨提升。如果删除“胜利”代码,则它没有透明度(很明显)。我尝试添加所有可以想到的内容以使画布透明,并且可以更改颜色但不能使其透明。


import matplotlib
# make sure Tk backend is used
matplotlib.use("TkAgg")  
import matplotlib.pyplot as plt

# create a figure and some subplots
fig, ax = plt.subplots(figsize=(4,2))
ax.plot([2,3,5,1])
fig.tight_layout()

win = plt.gcf().canvas.manager.window

win.lift()
win.attributes("-topmost", True)
win.attributes("-transparentcolor", "white")

plt.show()

python matplotlib pyqt5 alpha-transparency
1个回答
0
投票

您必须使用在Linux上测试过的Qt标志:

from PyQt5 import QtCore


import matplotlib

# make sure Tk backend is used
matplotlib.use("Qt5Agg")
import matplotlib.pyplot as plt

# create a figure and some subplots
fig, ax = plt.subplots(figsize=(4, 2))
fig.patch.set_alpha(0.0)
ax.patch.set_alpha(0.0)

ax.plot([2, 3, 5, 1])
fig.tight_layout()

win = plt.gcf().canvas.manager.window
win.setAttribute(QtCore.Qt.WA_NoSystemBackground, True)
win.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)
win.setStyleSheet("background:transparent")

plt.show()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.