How to have a transparent figure with a darkgrid background in seaborn?

Xenocidist

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)

Figure as shown in the notebook (with the darkgrid) Figure as shown in the notebook (with the darkgrid).

Figure as it looks saved (no background) Figure as it looks saved (no background)

Francisca Concha-Ramírez

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.

edited at
0

Comments

0 comments
Login to comment

Related

How to have transparent background for button?

jQuery Mobile - How to have a transparent background for the slider?

List in NSPopover on macOS: cannot figure out how to set background transparent

Making figure transparent with colored background

How to make button have transparent border that will show background of element below

How to convert the background to transparent?

How to put a matplotlib figure and a seaborn figure into one combined matplotlib figure

How to have a transparent JTextField?

Vuejs fusioncharts have completely transparent background

Style table to have transparent background with opaque text?

Make svg logo have transparent background

How to make UINavigationBar background transparent?

How to set a transparent background of JPanel?

How to set a control to a transparent background

How to cut out transparent background?

How to make the background of a JCheckBox transparent?

How to set transparent background of JDialog

How to set a tab background as transparent?

How to create a map with transparent background?

How to make jTextArea Transparent background

How to snapshot a WKWebView with a transparent background?

How to make image background transparent?

How to use an image with a transparent background?

How to achieve transparent background in ChoiceChip?

How to change background color to transparent?

how to make Background color transparent

How to make the canvas background transparent

How to set a button background to transparent?

How to overlay seaborn heatmap on matplotlib figure