Matplotlib: How to make the background transparent?
10,275
Solution 1
If you save the plot as an image you can set the background to be transparent
myploy.savefig('plotname.png', transparent=True)
Solution 2
Transparency is a window property and will thus depend on the backend in use and the operating system. Tkinter is not well suited to make transparent windows, but since the use of Qt is excluded in the question, the best you can get is probably something like the following, where the trick would be to turn everything that is white in the window transparent.
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()
Author by
vss
Updated on June 08, 2022Comments
-
vss almost 2 years
I came across ways to make the plot itself transparent, but how do I make the background transparent? Is there a way to do this without Qt? I want the plot to be over the background window, for example, say I'm running Chrome, I want the plot to be over the chrome window, with its contents visible.