How Set the theme of Seaborn Plots

In this article, we are going to learn How Set the theme of Seaborn Plots set the Background Color of a plot when using the Seaborn module. We will learn this with the help of coding examples and the visual output in the form of a plot by using some sample data.We are going to learn about a new function that is available in the seaborn module. These functions help us set the background color of your desired choice in a seaborn plot.

seaborn.set_theme() Function


Syntax for seaborn.set_theme() Function is:

seaborn.set_theme(context='notebook', style='darkgrid', palette='deep', font='sans-serif', font_scale=1, color_codes=True, rc=None)

1. How to use seaborn.set_theme() Function to set background color


In this example, we are going to use the seaborn.set_theme() Function to set the default theme or color for the plot. This is simple and just needs the seaborn.set_theme() Function to be called with no arguments.

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as pltsw

array = [[1,10,0,2,0],
         [3,5,0,1,0],
         [0,11,3,0,2],
         [0,0,12,0,0],
         [0,9,0,13,0],
         [0,13,0,0,11]]

DetaFrame_multiplot = pd.DataFrame(array, range(6), range(5))
sns.set_theme()
sns.lineplot(data=DetaFrame_multiplot)
pltsw.show()

Output

2. How to use seaborn.set_theme() Function to set grid and palette


Another example of seaborn.set_theme() Function with arguments like style and palettes.Here we are passing the style as “whitegrid” and palette as “pastel”.

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as pltsw

array = [[1,10,0,2,0],
         [3,5,0,1,0],
         [0,11,3,0,2],
         [0,0,12,0,0],
         [0,9,0,13,0],
         [0,13,0,0,11]]

DetaFrame_multiplot = pd.DataFrame(array, range(6), range(5))
sns.set_theme(style="whitegrid", palette="pastel")
sns.lineplot(data=DetaFrame_multiplot)
pltsw.show()

Output

3. How to use seaborn.set_theme() Function with custom theme


In this example, we are trying to make our own custom theme, where we are eliminating the right border and top border of the plot.
First, we are making this custom theme as a dictionary of key-value pair,s and then when we are calling the set_theme() function we are passing this dictionary to the RC parameter. Check this in the below code example.

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as pltsw
array = [[1,10,0,2,0],
[3,5,0,1,0],
[0,11,3,0,2],
[0,0,12,0,0],
[0,9,0,13,0],
[0,13,0,0,11]]
DetaFrame_multiplot = pd.DataFrame(array, range(6), range(5))
custom_params = {"axes.spines.right": False, "axes.spines.top": False}
sns.set_theme(style="ticks", rc=custom_params)
sns.lineplot(data=DetaFrame_multiplot)
pltsw.show()

Output: