Change Inner and Outer Background Colors in Matplotlib

As a Python developer, working on a Python data visualization project for a U.S.-based retail analytics dashboard, I needed to make my Matplotlib charts more visually appealing. The problem was simple: the default white background wasn’t blending well with the dark-themed dashboard.

So, I decided to change both the inner background color (the plot area) and the outer background color (the figure area) in Matplotlib.

If you’ve ever wondered how to customize the background colors of your Matplotlib plots, you’re in the right place. In this tutorial, I’ll show you simple and practical methods to change the inner and outer background colors in Matplotlib using Python.

Understand Background Colors in Matplotlib

Before we jump into the code, it’s important to understand that a Matplotlib plot has two main background areas:

  1. Figure background (outer color) – This is the area around the entire plot, including the space outside the axes.
  2. Axes background (inner color) – This is the area inside the plot where your data points, gridlines, and labels appear.

By changing both, you can create professional and visually consistent charts that match your project’s theme.

Method 1 – Change Inner Background Color Using Axes Facecolor

The simplest way to change the inner background color in Matplotlib is by setting the facecolor parameter of the Axes object. I often use this method when I want to highlight the data area with a specific color while keeping the rest of the figure default.

Here’s the Python code to do it:

import matplotlib.pyplot as plt

# Create a figure and axis
fig, ax = plt.subplots()

# Set the inner background color (axes facecolor)
ax.set_facecolor('#E8F6F3')  # light teal color

# Plot sample data
x = [1, 2, 3, 4, 5]
y = [10, 12, 8, 15, 10]
ax.plot(x, y, color='darkblue', linewidth=2)

# Add title and labels
ax.set_title('Sales Growth Over 5 Years', fontsize=14)
ax.set_xlabel('Year')
ax.set_ylabel('Sales (in Millions)')

# Show the plot
plt.show()

You can see the output in the screenshot below.

Change Inner and Outer Background Colors Matplotlib

In this code, I used the set_facecolor() method to change the inner background color of the axes. This method only affects the plot area (where the data is drawn) and not the space around it.

Method 2 – Change Outer Background Color Using Figure Facecolor

Now, let’s move on to the outer background color, which is the area around the axes, also known as the figure background. This is especially useful when embedding your chart in a web dashboard or a Power BI report where you want the entire figure to match a specific color theme.

Here’s how you can do it in Python:

import matplotlib.pyplot as plt

# Create a figure and axis
fig, ax = plt.subplots()

# Set the outer background color (figure facecolor)
fig.patch.set_facecolor('#FDF2E9')  # light beige color

# Plot data
x = [1, 2, 3, 4, 5]
y = [5, 7, 9, 6, 8]
ax.plot(x, y, color='crimson', linewidth=2)

# Add title and labels
ax.set_title('Monthly Revenue Trend', fontsize=14)
ax.set_xlabel('Month')
ax.set_ylabel('Revenue (in $1000s)')

# Show the plot
plt.show()

You can see the output in the screenshot below.

Change Inner and Outer Background Colors in Matplotlib

In this example, I used fig.patch.set_facecolor() to modify the outer background color of the figure. This method is perfect when you want the entire figure (including margins) to have a unified color.

Method 3 – Change Both Inner and Outer Background Colors Together

In real-world Python projects, you’ll often want to change both the inner and outer background colors together for a consistent look.

Here’s how you can do it in one go:

import matplotlib.pyplot as plt

# Create a figure and axis
fig, ax = plt.subplots(figsize=(7, 5))

# Set both inner and outer background colors
fig.patch.set_facecolor('#F5EEF8')  # outer background (light lavender)
ax.set_facecolor('#EBF5FB')         # inner background (light blue)

# Plot sample data
x = [10, 20, 30, 40, 50]
y = [25, 30, 28, 35, 40]
ax.plot(x, y, marker='o', color='navy', linewidth=2)

# Customize title and labels
ax.set_title('Customer Satisfaction Index', fontsize=14)
ax.set_xlabel('Survey Period')
ax.set_ylabel('Satisfaction Score')

# Display the plot
plt.show()

You can see the output in the screenshot below.

Matplotlib Change Inner and Outer Background Colors

In this Python example, I combined both methods to set colors for the figure and axes simultaneously. This gives you complete control over how your chart appears visually, great for dashboards and presentations.

Method 4 – Change Background Color Using rcParams (Global Settings)

If you’re creating multiple plots and want them all to have the same background color, manually setting colors for each figure can be time-consuming. In such cases, I prefer using Matplotlib’s rcParams, a global configuration system that lets you define default styles for all plots.

Here’s the Python code for that:

import matplotlib.pyplot as plt

# Set global background colors
plt.rcParams['figure.facecolor'] = '#FEF9E7'  # outer background
plt.rcParams['axes.facecolor'] = '#EAF2F8'    # inner background

# Create multiple plots
fig, ax = plt.subplots(1, 2, figsize=(10, 4))

# Plot data
ax[0].plot([1, 2, 3], [4, 5, 6], color='green', linewidth=2)
ax[1].plot([1, 2, 3], [6, 5, 4], color='red', linewidth=2)

# Add titles
ax[0].set_title('Store A Performance')
ax[1].set_title('Store B Performance')

# Show the plots
plt.show()

You can see the output in the screenshot below.

Change Matplotlib Inner and Outer Background Colors

This method is efficient when you’re building multiple plots in a Python script or a Jupyter Notebook.mBy setting global defaults, you ensure all your charts have a uniform background color without repeating code.

Method 5 – Change Background Color Dynamically (Using Loops)

In some Python applications, especially dashboards or automated reports, you may want to change background colors dynamically based on data or conditions.

Here’s how you can achieve that with a simple loop:

import matplotlib.pyplot as plt

# Define color themes
themes = [
    {'figure': '#FDEDEC', 'axes': '#F9EBEA'},
    {'figure': '#E8F8F5', 'axes': '#D1F2EB'},
    {'figure': '#FEF5E7', 'axes': '#FCF3CF'}
]

# Generate multiple plots with different background colors
for i, theme in enumerate(themes):
    fig, ax = plt.subplots()
    fig.patch.set_facecolor(theme['figure'])
    ax.set_facecolor(theme['axes'])
    ax.plot([1, 2, 3, 4], [i+1, i+2, i+3, i+4], linewidth=2)
    ax.set_title(f'Plot {i+1} with Theme {i+1}')
    plt.show()

I use this approach when creating Python scripts that generate multiple reports for different clients or business regions.

Additional Tip – Save Plot with Background Color

Sometimes, when you save your plot as an image, the background color may not appear unless explicitly defined.

Here’s how to ensure your background colors are preserved when saving:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
fig.patch.set_facecolor('#F4ECF7')
ax.set_facecolor('#EBDEF0')
ax.plot([1, 2, 3], [5, 7, 9], color='purple', linewidth=2)
ax.set_title('Saved Plot with Custom Background')

# Save the plot with background colors
plt.savefig('custom_background_plot.png', facecolor=fig.get_facecolor())
plt.show()

This ensures your saved image looks exactly like what you see on screen. I recommend this especially when preparing reports or presentations for clients.

Conclusion

So, that’s how I change both inner and outer background colors in Matplotlib using Python.

Whether you’re working on a professional analytics dashboard, a business presentation, or a machine learning visualization, customizing your chart’s background can help you create cleaner, more impactful visuals.

I personally use the rcParams method when building multiple charts and the set_facecolor method for quick one-off plots. Try these methods in your next Python project, and you’ll be surprised how much better your charts look with just a few lines of code.

You may also like to read other tutorials:

Leave a Comment

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.