The default behavior when saving a transparent image is to set the background to be transparent, but I want the background as is, and the white space surrounding the plot in the figure to be transparent.
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)
You can update matplotlib
's rcParams
to make the figure background transparent but keep the grid active, adding this to your code:
from matplotlib import rc
matplotlib.rcParams['axes.grid'] = True
matplotlib.rcParams['savefig.transparent'] = True
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments