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.
How to change background color in matplotlib?
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.
Examples
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:
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.
1. Custom face color for axes object
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:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
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.
2. Set a default axes face color for all plots
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:
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()
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.
Tutorials on matplotlib –
- Change Background Color of Plot in Matplotlib
- Change Font Size of elements in a Matplotlib plot
- Matplotlib – Save Plot as a File
- Change Size of Figures in Matplotlib
- Plot a Bar Chart using Matplotlib
- Plot a Pie Chart with Matplotlib
- Plot Histogram in Python using Matplotlib
- Create a Scatter Plot in Python with Matplotlib
- Plot a Line Chart in Python with Matplotlib
- Save Matplotlib Plot with Transparent Background
- Change Font Type in Matplotlib plots