HOWTO · Seaborn
How to Set the Background Color of Seaborn Plots
This tutorial demonstrates how to set the background color of Seaborn plots in Python. Learn various methods to customize your plots, including using set_style, set_palette, and direct figure background settings. Enhance your data visualizations with stunning backgrounds and make your insights stand out. Explore techniques for creating cohesive and visually appealing graphics that effectively communicate your data story.
On this page
To set an exact Seaborn plot background, pass Matplotlib’s axes.facecolor and figure.facecolor settings to seaborn.set(). The axes setting colors the plotting area; the figure setting colors the surrounding canvas. For a quick coordinated look, choose a preset with seaborn.set_style() instead. Use the exact-color approach when you need a particular color, and a preset when its combined background, grid, and tick styling suits your plot.
Set an Exact Background Color with Seaborn and Matplotlib
Pass an rc dictionary to seaborn.set() with the Matplotlib settings you want to change. Color names and hex strings are both accepted. This self-contained example uses a pale blue plotting area and a near-white figure canvas, then saves the figure:
Run this example:
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(rc={
"axes.facecolor": "#EAF2F8",
"figure.facecolor": "#F7F9FC",
})
fig, ax = plt.subplots()
sns.scatterplot(x=[1, 2, 3], y=[2, 4, 3], ax=ax)
fig.savefig("seaborn-background.png", dpi=150, bbox_inches="tight")
Change either color value to a named color such as lightblue or to another hex color. The settings apply to plots created after the call; for a single axes or figure, set its face color directly as described below.
Choose a Background with a Seaborn Style
seaborn.set_style() selects a preset that controls several visual properties together. The available styles are darkgrid, whitegrid, dark, white, and ticks. For example, sns.set_style("whitegrid") gives the axes a light background with grid lines, while sns.set_style("dark") uses a dark plotting area without the grid. A style changes more than the background, so choose one whose grid and ticks also fit the data.
A palette controls the colors assigned to data series, and context adjusts scaling such as label and line sizes. sns.set_palette("deep") and sns.set_context("notebook") can coordinate a plot’s marks and text with its style, but neither sets the background color. For one consistent setup, sns.set() can accept style, palette, context, and rc settings together.
Set Figure and Axes Backgrounds Directly
Use Matplotlib objects when a color should affect only one figure or one plotting area. After fig, ax = plt.subplots(), fig.patch.set_facecolor("#F7F9FC") changes the whole figure canvas, including the margin around the axes. ax.set_facecolor("#EAF2F8") changes only that axes’ plotting area. These methods are useful when different axes in a multi-plot figure need different backgrounds; call set_facecolor() on each axes that needs a separate color.
The figure.facecolor and axes.facecolor values passed through Seaborn provide defaults for figures and axes created afterward. Direct setters target the specific objects you already have, which makes their scope clear and avoids changing later plots.
Coordinate Plot Styling, Save the Figure, and Check Readability
The example above combines an exact background with a Seaborn scatter plot and saves it through fig.savefig(). Call the figure’s save method after creating the plot and setting its colors. If using plt.savefig() instead, do so while the intended figure is current. Saving does not require displaying the plot first.
Keep enough contrast between the background, data marks, labels, and grid lines. A dark background may work well with light marks, while a light background usually supports dark marks; check that grid lines remain visible without competing with the data. Seaborn accepts standard Matplotlib color specifications, including named colors and hex strings, so use colors that preserve legibility in the saved image as well as on screen.