 # How to Change Background Color of Plot in Matplotlib : 4 Steps Only

### GET FREE AMZAON AUDIOBOOKS      Do you want to change the color of the background of the plot in Matplotlib? If yes then this post is for you. In this entire tutorial, you will know how to change the background color of both axes and plot using Matplotlib.

## Steps to change Matplotlib background color

In this section, you will know all the steps required to implement this tutorial. Please make sure that you do all the coding demonstrations on the Jupyter notebook as I am also doing the same on Notebook.

### Step 1: Import all the required libraries

The first and the most basic step is to import all the necessary libraries. Here I am using only two libraries. One is NumPy for data creation and the other is Matplotlib for plotting the data. Let’s import them using the import statement.

``````import matplotlib.pyplot as plt
import numpy as np``````

### Step 2: Create data points for plotting

Now let’s create a data point for changing matplotlib background color. To do so I am creating x and y variables and assigning them with the NumPy array. You can create NumPy array using the numpy.array() method. Let’s create them.

``````x = np.array([1,2,3,4,5])
y = np.array([0.1,0.2,0.3,0.4,0.5])``````

### Step 3: Plot the Datapoints

After the creation of the sample data points, let’s plot them. Execute the below lines of code.

``````import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
x = np.array([1,2,3,4,5])
y = np.array([0.1,0.2,0.3,0.4,0.5])
plt.figure(figsize=(8,8))
plt.plot(x,y)``````

Here I am using %matplotlib inline for plotting the figure inline. And also modifying the size of the plot using the figsize. When you will run the code you will get the following output.

Output

### Step 4: Change the Matplollib Background color

Now the last step is to change the background color for the Maplotlib plot. There are two ways you can do so. You can change the background color of the plot. And the other way to change the axes only. Let’s implement both of them.

#### Change the background color of the plot

You can change the background color of the plot by changing the rcParams[‘axes.facecolor’]. Just execute the following lines of the code and see the output.

``````import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
x = np.array([1,2,3,4,5])
y = np.array([0.1,0.2,0.3,0.4,0.5])
plt.rcParams['axes.facecolor'] = 'green'
plt.figure(figsize=(8,8))
plt.plot(x,y,color="white")``````

Output

You can see I am changing the background color from white to green. In the next section, you will know to change the axis color only.

#### Changing the background color of the axes

You can change the background color of the axes by using the set_facecolor(‘yellow’) method. For example, I want to change the axes color to yellow. Then I will run the following lines of code.

``````import matplotlib.pyplot as pt
import numpy as np
%matplotlib inline
x = np.array([1,2,3,4,5])
y = np.array([0.1,0.2,0.3,0.4,0.5])
fig = pt.figure()
fig.set_facecolor('yellow')
pt.plot(x,y)``````

Output

You can see the background color of the axes has been changed to yellow color.

These are the methods to change Matplotlib background color. Hope this tutorial has solved your queries. If you have any other queries then you can contact us for more help.

Source:

matplotlib 