How to Change Background Color in Matplotlib

Being a Python developer, I’ve worked extensively with data visualization libraries, and Matplotlib remains one of my go-to tools. One common customization I often need is changing the background color of my plots.

In this article, I’ll share simple, effective ways to change the background color in Matplotlib. I’ll walk you through different methods based on real-world experience, making it easy for you to apply them in your projects.

Let’s get in!

Method to Change Background Color in Matplotlib

Now, I will explain the methods to change the background color in Matplotlib.

Read Matplotlib Scatter Marker

1: Change the Figure Background Color

The simplest way to change the background color is by modifying the figure’s face color. This changes the entire plot area’s background.

Here’s how you do it:

import matplotlib.pyplot as plt

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

# Set the figure background color to light grey
fig.patch.set_facecolor('lightgrey')

# Plot sample data
ax.plot([10, 20, 30, 40], [1, 4, 9, 16])

plt.show()

You can see the output in the screenshot below.

matplotlib background color

In this example, I set the figure’s background to light grey. You can use any Matplotlib-supported color name, hex code (e.g., #f0f0f0), or RGB tuple.

This method is great when you want to change the whole canvas background, including the space outside the plot area.

2: Change the Axes Background Color

Sometimes, you only want to change the background inside the plotting area (the axes), leaving the surrounding figure background intact.

You can do this by setting the axes’ face color:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

# Set the axes background color to light blue
ax.set_facecolor('#e6f2ff')

ax.plot([5, 15, 25, 35], [2, 3, 5, 7])

plt.show()

You can see the output in the screenshot below.

pyplot background color

This method is perfect when you want to highlight the plotting area, for example, to distinguish it from the rest of the figure or when embedding plots into reports with colored backgrounds.

Check out Matplotlib Dashed Line

3: Change Both Figure and Axes Background Colors

For maximum customization, you might want to change both the figure and axes backgrounds to different colors.

Here’s an example:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

# Set figure background to black
fig.patch.set_facecolor('black')

# Set axes background to dark grey
ax.set_facecolor('#333333')

ax.plot([1, 2, 3, 4], [10, 20, 25, 30], color='white')

plt.show()

You can see the output in the screenshot below.

matplotlib background color transparent

This approach is useful when creating dark-themed plots, which are popular in dashboards or presentations shown in dark rooms.

4: Change Background Color Using plt.style

Matplotlib comes with several built-in styles, and you can even create your own. Some styles come with different background colors.

For example, using the dark_background style changes the figure and axes background to dark colors automatically:

import matplotlib.pyplot as plt

plt.style.use('dark_background')

plt.plot([100, 200, 300, 400], [1, 3, 2, 5], marker='o')

plt.show()

This is a quick way to switch to a dark theme without manually setting colors.

You can also customize styles or create your style sheet to maintain consistent background colors across multiple plots.

Read Matplotlib plot_date

5: Change Background Color for Subplots Individually

When working with multiple subplots, you might want to set different background colors for each subplot.

Here’s how:

import matplotlib.pyplot as plt

fig, axs = plt.subplots(2, 2)

colors = ['#ffcccc', '#ccffcc', '#ccccff', '#ffffcc']

for ax, color in zip(axs.flat, colors):
    ax.set_facecolor(color)
    ax.plot([1, 2, 3], [1, 4, 9])

plt.show()

This method is handy for comparing different datasets visually, where each subplot has a distinct background color.

Check out the Matplotlib Subplot Tutorial

Tips for Choosing Background Colors

  • Contrast: Ensure your plot elements (lines, markers, text) contrast well with the background. For dark backgrounds, use light colors for lines and text and vice versa.
  • Consistency: If you’re preparing multiple plots for a report or dashboard, maintain consistent background colors for a professional look.
  • Accessibility: Consider color blindness and readability when selecting colors. Tools like ColorBrewer can help pick colorblind-safe palettes.

Changing the background color in Matplotlib is easy, but it can significantly improve your data visualization. Whether you want a subtle color change or a dramatic dark theme, these methods cover most use cases you’ll encounter.

Feel free to experiment with these techniques in your next project, whether you’re analyzing retail sales data in Chicago or visualizing weather trends across the US. Properly customized plots not only look better but also communicate your insights more effectively.

Other Python articles you may like:

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.