Change Background Color of Plot in Matplotlib

In this tutorial, we’ll look at how to change the background color of plots in matplotlib. Note that, the background color here means the background color of the region bounded by the axes. Also referred to as the axes face color.

To change the background color of matplotlib plots, you can use the set_facecolor() function of the axes object of the plot. You can also set a global face color for all plots using rcParams. (See the syntax and examples below). The following is the syntax:

# set the face color of an axes object
ax.set_facecolor('orange')
# set the face color globally for all axes objects
plt.rcParams.update({'axes.facecolor':'orange'})

The above syntax assumes matplotlib.pyplot is imported as plt.
In the above syntax, we first have the code to set the face color of a specific axes object and then we have the code to set the face color globally for all plots.

Let’s look at some examples of using the above syntax to change the background color in matplotlib plots. First, we’ll create a simple matplotlib line plot and see the default face color of the plot.

import matplotlib.pyplot as plt
# reset the plot configurations to default
plt.rcdefaults()

# number of employees of A
emp_count = [3, 20, 50, 200, 350, 400]
year = [2014, 2015, 2016, 2017, 2018, 2019]

# plot a line chart
fig, ax = plt.subplots()
ax.plot(year, emp_count)
# set axis titles
ax.set_xlabel("Year")
ax.set_ylabel("Employees")
# set chart title
ax.set_title("Employee Growth at A")
plt.show()

Output:

Line chart in matplotlib with default face color

You can see that in the above plot the default face color is white. Let’s go ahead and add a custom face color to our plot. There are multiple ways to add custom axes face color to our plots – You can set a custom face color individually for your axes objects or you can add a default face color for all the plots using rcParams.

To set the face color of individual plots, use the axes object’s set_facecolor() function.

# plot a line chart with custom axes face color
fig, ax = plt.subplots()
ax.plot(year, emp_count)

# set axis titles
ax.set_xlabel("Year")
ax.set_ylabel("Employees")
# set chart title
ax.set_title("Employee Growth at A")
# set axes facecolor
ax.set_facecolor("orange")
plt.show()

Output:

Line plot with orange face color

In the above example, we set the face color of the ax object to “orange” and you can see that the resulting plot has an orange background in region bounded by the axes.

You can also set the default axes face color to be used in all your plots by updating the rcParams configurations.

# reset the plot configurations to default
plt.rcdefaults()
# set the axes color glbally for all plots
plt.rcParams.update({'axes.facecolor':'lightblue'})

# plot a line chart
fig, ax = plt.subplots()
ax.plot(year, emp_count)
# set axis titles
ax.set_xlabel("Year")
ax.set_ylabel("Employees")
# set chart title
ax.set_title("Employee Growth at A")
plt.show()

Output:

Line plot with light blue face color

Note that first, we used the rcdefaults() function to reset the matplotlib configurations to their defaults, and then went ahead to update our default axes face color to “lightblue”. The resulting plot has a “lightblue” axes face color.

Since we have updated the default axes face color, all the following plots will have the same axes face color (“lightblue” in our case). Let’s create a different plot to see this in action.

# plot a line chart
fig, ax = plt.subplots()
ax.scatter(year, emp_count)
# set axis titles
ax.set_xlabel("Year")
ax.set_ylabel("Employees")
# set chart title
ax.set_title("Employee Growth at A")
plt.show()
Scatte plot with light blue face color

The above scatter plot has “lightblue” axes face color.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having matplotlib version 3.2.2


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.