Matplotlib:如何使背景透明?

3

我发现有一些方法可以使绘图本身透明,但如何使背景透明呢? 有没有不使用Qt的方法来实现这个目标? 我希望绘图在背景窗口上方,例如,假设我正在运行Chrome浏览器,我希望绘图位于Chrome窗口之上,并且能够显示其内容。


2
可能是如何从matplotlib导出具有透明背景的图形?的重复问题。 - user1211
2个回答

6

如果您将图表保存为图像,则可以将背景设置为透明。

myploy.savefig('plotname.png', transparent=True)

4

透明度是窗口属性,因此取决于使用的后端和操作系统。Tkinter不适合制作透明窗口,但由于问题中排除了Qt的使用,您可能最好的选择是类似下面的东西,其中的技巧是将窗口中所有白色变成透明。

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

enter image description here


如何使Matplotlib图形透明 - David
1
当我尝试这个时,我遇到了以下错误:tkinter.TclError: 错误的属性“-transparentcolor”:必须是-alpha、-topmost、-zoomed、-fullscreen或-type。 - hm8
发生错误,AttributeError: 'FigureCanvasAgg' 对象没有属性 'manager' - rosefun

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