Set the Background Color of Seaborn Plots

Set the Background Color of Seaborn Plots

Manav Narula Jul-16, 2021 May-13, 2021 Seaborn Seaborn Color
  1. Use the seaborn.set() Function to Change the Background Color of Seaborn Plots in Python
  2. Use the seaborn.set_style() Function to Change the Background Color of Seaborn Plots in Python

This tutorial will introduce how to change the background color of plots created using the seaborn module in Python.

Use the seaborn.set() Function to Change the Background Color of Seaborn Plots in Python

The set() function adds different elements and configures the aesthetics of the plot. There is no direct argument or method to change background color in seaborn. Since the seaborn module is built on the matplotlib module, we can use parameters from that module for seaborn plots. We can pass them to the rc parameter in the set() function as dictionary key-value pairs.

We will use the axes.facecolor and figure.facecolor parameters to alter the background color. The axes are considered the canvas where the graph is drawn, and the figure is the overall object containing the different axes objects.

We can use the above method as shown below.

import random
import seaborn as sns
import matplotlib as plt

s_x = random.sample(range(0,100),20)
s_y = random.sample(range(0,100),20)

sns.set(rc={'axes.facecolor':'cornflowerblue', 'figure.facecolor':'cornflowerblue'})
sns.scatterplot(y = s_y, x = s_x)

seaborn background color

Use the seaborn.set_style() Function to Change the Background Color of Seaborn Plots in Python

There are many different themes available in the seaborn module. These themes may not be the exact solution to our problem, but it allows us to customize the figures’ background a little bit.

The set_style() function is used to set the theme for the plot. It can accept the following values - dark, white, whitegrid, darkgrid and tickers. The dark and darkgrid parameters provide a grey background without and with grids, respectively. Similarly, we can conclude for the white and whitegrid parameters.

For example,

import random
import seaborn as sns
import matplotlib as plt

s_x = random.sample(range(0,100),20)
s_y = random.sample(range(0,100),20)

sns.set_style("darkgrid")
sns.scatterplot(y  = s_y, x = s_x)

set seaborn background color with the set_style() function

Author: Manav Narula
Manav Narula avatar Manav Narula avatar

Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.

LinkedIn

Related Article - Seaborn Color

  • Create Different Color Palette Using Seaborn