How to Set Background Color of Seaborn Plots

In this article, we are going to learn how to Set Background Color of Seaborn Plots by using 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 two functions that are available in the seaborn module. These functions help us set the background color of your desired choice in a seaborn plot. These functions are:

  • seaborn.set() Function.
  • seaborn.set_style() Function.

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


In this example, we are going to use the seaborn.set() function to set the background color in a plot and also we will see how to set the color on the axes. Let us see an example of a seaborn lineplot and background color modifications.

Note: seaborn.set() Function will be soon replaced with seaborn.set_theme() Function. So if you want to read about this function,
please check it here.

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(rc={'axes.facecolor':'orange', 'figure.facecolor':'green'})
sns.lineplot(data=DetaFrame_multiplot)
pltsw.show()

Output

Explanation:

  • We are importing the seaborn module to our program.
    • import seaborn as sns
  • We are importing the panda’s module to our program.
    • import pandas as pd
  • We are importing the matplotlib module’s pyplot to our program to display the plot.
    • import matplotlib.pyplot as pltsw
  • We are taking a sample array of random values.
    • 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]]
  • We are next converting this array to a panda dataframe with size of 6 rows and 5 columns.
    • DetaFrame_multiplot = pd.DataFrame(array, range(6), range(5))
  • We are using the sns.set() function and passing the arguments for the axes facecolor and the plot background color.To do this we are passing argument for rc parameter. It takes a dictionary with key value pair.
    • sns.set(rc={‘axes.facecolor’:’orange’, ‘figure.facecolor’:’green’}) In this line of code , first key value pair we are passing is the axes facecolor which is the plot canvas where we are ploting the line plot.This will be orange in color.
    • Second key value pair is figure facecolor which is the area of the plot where data is represented. This will be green in color.
  • Finally, we are calling the lineplot() function on this dataframe , by just passing this dataframe to lineplot() function.
    • sns.lineplot(data=DetaFrame_multiplot) The lineplot() function will act on the values that are in the columns of the data frame as the y-axis values.
      It will use the index of the values as the x-axis values to plot the lines.
  • The last and important step is to call the show function to show the plot on screen.
    • pltsw.show()

2. How to use seaborn.set_style() Function to set background-color


In this example, we are going to use the seaborn.set_style() function to set the background color in a plot and also
we will see how to set the color on the axes.The seaborn.set_style() function is little different from seaborn.set() function because it is used to set the theme of the plot.It means this function have only limited options that you can use for your plot.Some of the possible theme options that seaborn.set_style() function support are white, dark,darkgrid,whitegrid and tickers.Let us see an example of a seaborn line plot and background color modifications.

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_style("darkgrid")
sns.lineplot(data=DetaFrame_multiplot)
pltsw.show()

Output