如何在Seaborn中使用带有暗网格背景的透明图形?

时间:2019-11-19 14:12:03

标签: matplotlib seaborn

保存透明图像时的默认行为是将背景设置为透明,但是我希望背景保持原样,并且图中图形周围的空白也应透明。

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)

如图所示的笔记本(带有深色网格) Figure as shown in the notebook (with the darkgrid).

该图看起来已保存(无背景) Figure as it looks saved (no background)

1 个答案:

答案 0 :(得分:0)

您可以更新matplotlib的{​​{1}},使图形背景透明,但保持网格处于活动状态,将其添加到代码中:

rcParams