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

标签 python matplotlib pyqt5 alpha-transparency

我需要整个绘图窗口是透明的,这样我的桌面上的 chrome 窗口就可以透过绘图看到,这样我就可以在看到它背后的东西的同时向它添加点。

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

我上面列出的答案正是我想要做的,除了我的交互系统不能与 TK 一起工作。我想使用 Qt5Agg。当我运行上面的代码时,系统不会接受它——它说 QT5 当前正在运行。如果我在没有加载 QT 的情况下运行它,它会创建一个空白的透明窗口(耶!)但是如果我移动它或单击该图标,它会变成不透明的黑色,没有任何绘图。如果我将 tk 更改为 Qt5,它会在电梯上提示。如果我删除“win”代码,它就没有透明度(很明显)。我已经尝试添加所有我能想到的东西来使 Canvas 透明,我可以更改颜色但不能使其透明。


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()

当我进行以下建议的更改时:eyllanesc

我在 Vanilla Spyder 4.1.3 中找到了 | Python 3.7.7 64 位 | Qt 5.9.6 | PyQt5 5.9.2 | Windows 10

为了导入QtCore,我必须先

  • conda 安装pyqt

  • 不够,那么conda install pyqt5

  • 还有 conda update --all

当我这样做时,代码运行没有错误。 这是更好的第一个结果!,但我仍然只得到卡住的 mpl.fig 窗口。然而,这一次,它是白色的。 . .控制台返回,但 mpl 窗口挂起。再次运行,一个新的卡住窗口。重新启动并再次运行:同样的结果。

我希望这是一个简单的错误;请教这位新手。

@eyllanesc
修订:Python 屏幕跟踪应用程序 – 需要一个几乎透明的绘图窗口。 我需要整个绘图窗口是透明的,以便可以通过绘图看到我桌面上的 chrome 窗口,这样我就可以向它添加绘图 (x, y) 点,同时看到它背后的内容。

添加命令 win.setWindowFlags(QtCore.Qt.FramelessWindowHint) 确实使窗口透明,但它使工具栏透明,去掉了标题栏,并删除了移动或调整窗口大小。它还使图形区域对鼠标不敏感,除非我越界。我将 facecolor 属性添加到 subplots 命令,这样我就可以看到发生了什么。只要我为 fig-alpha 或 ax-alpha 设置一个非零值,图形就会对整个区域的鼠标敏感。

我需要能够移动和调整窗口大小,并希望工具栏不透明或至少对整个工具栏上的鼠标敏感。你能帮忙吗?感谢过去的帮助!

## Python Code Fragment by Helen for Windows 10
## to test sequence creating plot with transparent
## background (to be used to trace and record xy pairs)

from PyQt5 import QtCore
import matplotlib
matplotlib.use("Qt5Agg")  #define backend, must be before pyplot is imported
import matplotlib.pyplot as plt

# create a figure and a subplot
fig,ax = plt.subplots(figsize=(4, 2),facecolor=(1.,1.,0.,0.1)) #facecolor of figure

fig.patch.set_alpha(0.1) 
ax.patch.set_alpha(0.1)

# plot some fixed points
ax.plot([2, 3, 5, 1])
fig.tight_layout()

#make window transparent to the desktop
win = plt.gcf().canvas.manager.window
win.setAttribute(QtCore.Qt.WA_NoSystemBackground, True)
win.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)
win.setStyleSheet("background:transparent")
win.setWindowFlags(QtCore.Qt.FramelessWindowHint) 
win.setWindowTitle("My App")

plt.show()

最佳答案

您必须使用在 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

关于python - Matplotlib:如何在 Windows 上使背景透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62110823/

相关文章:

python - 将 sklearn 半正矢输出解释为公里

python - 如何使用 Elixir/SQLAlchemy 进行原子递增/递减

python - Qlabels 在最后被剪掉

python - 如何通过 'try... except'函数避免所有可能的错误

python - 将列表拆分为均匀大小的重叠 block n-max

python - 在python中生成网络图

python - 如何使用 matplotlib 索引 n 组 4 列以绘制多个图?

python - 对直方图进行颜色编码

python - 如何让布局自动填充窗口大小

python - 当我希望在两个 setText 之间做某事时,pyqt5 QLineEdit.setText 仅在功能中工作一次