保存透明图像的默认行为是将背景设置为透明,但我希望保留原有背景,并使图中绘图周围的白色空间透明化。
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
%load_ext autoreload
%autoreload 2
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
tips = sns.load_dataset("tips")
# Set background of the charts' to be a dark grid.
sns.set_style("darkgrid")
g = sns.FacetGrid(tips, col="smoker", col_order=["Yes", "No"])
g = g.map(plt.hist, "total_bill")
# Save figure with a transparent background (but this unfortunately)
# overrides the dark grid, whereas I want the white surrounding the
# plots to be transparent.
plt.savefig("example.pdf", transparent=True)
如笔记本中所示的图形(带有暗网格)
![如笔记本中所示的图形(带有暗网格)](https://istack.dev59.com/oKpIy.webp)
如保存时所显示的图形(无背景)
![如保存时所显示的图形(无背景)](https://istack.dev59.com/FuBqz.webp)
facecolor=(1,1,1,0)
- ImportanceOfBeingErnestplt.savefig(*)
调用中吗?对我来说,这最终会再次出现白色背景。 - Xenocidisttransparent
参数。 - ImportanceOfBeingErnest