Change the Default Background Color in Matplotlib

While working on a Python data visualization project, I wanted to make my plots look more professional and visually appealing. The default white background in Matplotlib looked plain, and I wanted something that matched my presentation theme.

If you’ve ever felt the same, you’re not alone! Matplotlib provides us with full control over the appearance of our plots, including the background color.

In this tutorial, I’ll show you how to change the default background color in Matplotlib using different methods. We’ll go step-by-step, so even if you’re new to Python plotting, you’ll be able to follow along easily.

Why Change the Background Color in Matplotlib?

Before we get into the code, let’s understand why you might want to change the background color.

  • To highlight data better in presentations or reports
  • To match your company’s branding or theme
  • To improve readability in dark or light mode dashboards
  • To make your visualization stand out in Jupyter Notebooks or Streamlit apps

As a Python developer with over a decade of experience, I’ve learned that a small visual tweak, such as background color, can make a significant difference in how your chart conveys information.

Method 1 – Change the Background Color for a Single Plot

The simplest way to change the background color in Matplotlib is by using the set_facecolor() method. This allows you to customize the color of the figure or specific axes.

Here’s how I usually do it in Python:

import matplotlib.pyplot as plt

# Create a simple dataset
x = [1, 2, 3, 4, 5]
y = [10, 12, 8, 15, 10]

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

# Change background color of the figure
fig.set_facecolor('#f0f0f0')  # light gray

# Change background color of the axes
ax.set_facecolor('#e6f2ff')  # light blue

# Plot the data
ax.plot(x, y, color='blue', linewidth=2, marker='o')

# Add title and labels
ax.set_title('Sales Growth Over Time (USA Region)', fontsize=14)
ax.set_xlabel('Month')
ax.set_ylabel('Sales (in thousands)')

# Display the plot
plt.show()

I have executed the above example code and added the screenshot below.

Change the Default Background Color Matplotlib

In this Python example, I used a light gray background for the figure and a soft blue for the axes. This makes the chart easier on the eyes, especially when sharing in reports or slides.

Method 2 – Set the Default Background Color for All Plots

If you’re working on multiple charts, you might not want to repeat the same code for every plot. In that case, you can use Matplotlib’s rcParams to set a global background color.

Here’s how you can do it:

import matplotlib.pyplot as plt

# Set default background colors globally
plt.rcParams['figure.facecolor'] = '#222831'  # dark gray for figure
plt.rcParams['axes.facecolor'] = '#393e46'    # slightly lighter for axes
plt.rcParams['savefig.facecolor'] = '#222831' # ensures saved images match

# Create a sample plot
x = [10, 20, 30, 40, 50]
y = [100, 120, 90, 130, 110]

plt.plot(x, y, color='#00adb5', linewidth=3, marker='o')
plt.title('Website Traffic Growth (USA)', color='white', fontsize=14)
plt.xlabel('Days', color='white')
plt.ylabel('Visitors', color='white')

# Change tick colors for dark background
plt.tick_params(colors='white')

plt.show()

I have executed the above example code and added the screenshot below.

Change the Default Background Color in Matplotlib

With this Python code, every new plot you create will automatically use the new background colors. This is perfect for dashboards or automated reporting scripts where you want consistency across multiple charts.

Method 3 – Change Background Color When Saving the Plot

Sometimes the background looks fine on-screen but turns white when you save the plot as an image. That’s because Matplotlib doesn’t automatically apply the figure’s background color when saving.

To fix this, you can use the facecolor argument in the savefig() function.

Here’s an example:

import matplotlib.pyplot as plt

# Create data
x = [1, 2, 3, 4, 5]
y = [5, 7, 6, 8, 7]

# Create plot
fig, ax = plt.subplots()
ax.plot(x, y, color='orange', linewidth=2, marker='s')

# Set background colors
fig.set_facecolor('#2b2b2b')
ax.set_facecolor('#3c3f41')

# Customize text color for dark background
ax.set_title('Python Developer Productivity', color='white')
ax.set_xlabel('Week', color='white')
ax.set_ylabel('Tasks Completed', color='white')
ax.tick_params(colors='white')

# Save with background color
plt.savefig('developer_productivity.png', facecolor=fig.get_facecolor())

plt.show()

I have executed the above example code and added the screenshot below.

Change Matplotlib Default Background Color

In this example, I made sure the saved image retained the dark background. This is a common mistake many Python beginners make. The plot looks great in Jupyter, but the saved file turns white.

Method 4 – Use a Custom Style Sheet with Background Color

If you frequently use the same background and color scheme, you can create a custom Matplotlib style sheet. This is one of my favorite Python tricks because it keeps your code clean and reusable.

Here’s how to do it:

  1. Create a file named custom_dark.mplstyle in your project folder.
  2. Add the following lines to the file:
figure.facecolor : #1e1e1e
axes.facecolor : #2d2d2d
axes.edgecolor : white
axes.labelcolor : white
xtick.color : white
ytick.color : white
text.color : white
savefig.facecolor : #1e1e1e
  1. Now, load this style in your Python code:
import matplotlib.pyplot as plt

# Use the custom style
plt.style.use('custom_dark.mplstyle')

x = [1, 2, 3, 4, 5]
y = [15, 18, 14, 20, 17]

plt.plot(x, y, color='cyan', linewidth=2, marker='o')
plt.title('Python Data Visualization - Custom Theme')
plt.xlabel('Quarter')
plt.ylabel('Revenue (in Millions)')
plt.show()

This approach is great for teams. You can share the .mplstyle file so everyone uses the same visual theme for reports and dashboards.

Method 5 – Change Background Color Dynamically in Interactive Plots

If you’re using interactive environments like Jupyter Notebook or Streamlit, you can dynamically change background colors based on user input. This can make your Python dashboards more engaging.

Here’s a simple Jupyter example:

import matplotlib.pyplot as plt
from ipywidgets import interact

def update_plot(bg_color):
    fig, ax = plt.subplots()
    fig.set_facecolor(bg_color)
    ax.set_facecolor('white')
    ax.plot([1, 2, 3, 4], [10, 20, 15, 25], color='blue', marker='o')
    ax.set_title(f'Interactive Background: {bg_color}')
    plt.show()

interact(update_plot, bg_color=['white', '#f0f0f0', '#d3e0ea', '#222831'])

In this Python example, you can pick a background color from a dropdown menu, and the plot updates instantly. It’s a fun and practical way to experiment with visualization aesthetics.

Tips for Choosing the Right Background Color

After years of working with data visualization in Python, here are a few quick tips I’ve learned:

  • Use light backgrounds for printed reports or documents.
  • Use dark backgrounds for dashboards or presentations.
  • Ensure text and line colors have enough contrast against the background.
  • Stick to neutral tones (gray, beige, light blue) for professional visuals.
  • Always test how your plot looks when exported to PNG or PDF.

A good rule of thumb: if your plot looks good in grayscale, it’s likely well-balanced in color too.

Conclusion

Changing the default background color in Matplotlib is a small but powerful way to improve your Python visualizations.
Whether you want to match your company’s branding, create dark-mode dashboards, or simply make your plots easier to read, Matplotlib gives you full flexibility.

In this tutorial, I showed you five different methods to achieve this:

  • Using set_facecolor() for single plots
  • Setting global defaults with rcParams
  • Saving plots with background colors
  • Creating custom style sheets
  • Changing colors dynamically in interactive plots

Each method has its own use case, and with a little experimentation, you can find the one that fits your workflow best.

You may like to read other articles on Matplotlib:

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.