Python Matplotlib Pie Chart Background Color

In my years of working with data visualization, I have found that a default white background rarely fits a professional dashboard.

A well-chosen background color makes your Python Matplotlib pie charts pop and aligns them with your brand’s aesthetic.

In this tutorial, I will demonstrate how to manipulate the background colors of your pie charts using various Python techniques.

The Need for Custom Backgrounds in Python Matplotlib

When I first started building data reports for US tech firms, I realized that the default gray or white backgrounds looked quite bland.

Adding a custom background color helps highlight the data segments, especially when presenting at a corporate meeting.

In Python, Matplotlib gives you granular control over two main areas: the Figure and the Axes. Understanding the difference between these two is the secret to mastering Matplotlib styling.

Method 1: Change the Figure Facecolor in Python

The most common way I change the background is by modifying the “facecolor” of the Figure object.

The Figure is the entire window or page where your plot sits. If you change this, the area outside the actual chart also changes.

Python Code Example: US Tech Market Share

In this example, I will visualize the market share of major US tech companies and apply a “Linen” background color to the entire figure.

import matplotlib.pyplot as plt

# Data representing US Tech Market Share (hypothetical)
labels = ['Apple', 'Microsoft', 'Alphabet', 'Amazon', 'Meta']
sizes = [35, 30, 15, 10, 10]
colors = ['#ff9999','#66b3ff','#99ff99','#ffcc99', '#c2c2f0']

# We create the figure and set the facecolor attribute
fig = plt.figure(figsize=(10, 7), facecolor='linen')

# Adding the pie chart to the figure
plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=140, colors=colors)

# Adding a title for context
plt.title('Market Share of Top US Tech Companies', fontsize=15)

# Display the plot
plt.show()

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

Matplotlib Pie Chart Background Color

When I run this Python script, the entire canvas turns into a soft linen color. It looks much more sophisticated than the default.

Method 2: Modify the Axes Patch Color

Sometimes, I don’t want to change the whole figure. I only want the area immediately behind the pie chart to have a specific color.

In Matplotlib, the “Axes” is the area where the data is plotted. We can access the background of this area using ax.set_facecolor().

Python Code Example: US Energy Consumption Sources

Let’s look at a breakdown of US energy consumption. I will set the background of the plotting area to a light gray to make the bright slices stand out.

import matplotlib.pyplot as plt

# Data for US Energy Consumption by Source
sources = ['Petroleum', 'Natural Gas', 'Renewable', 'Coal', 'Nuclear']
usage = [36, 32, 12, 11, 9]

# Create subplots to get access to the 'ax' object
fig, ax = plt.subplots(figsize=(10, 7))

# Set the figure facecolor (the outer border)
fig.set_facecolor('white')

# Set the axes facecolor (the area behind the pie)
ax.set_facecolor('#f0f0f0')

# Plotting the pie chart
ax.pie(usage, labels=sources, autopct='%1.1f%%', shadow=True, startangle=90)

# We must ensure the aspect ratio is equal to make it a circle
ax.axis('equal') 

plt.title('US Energy Consumption Breakdown', fontsize=16)
plt.show()

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

Pie Chart Background Color Python Matplotlib

I find this method incredibly useful when I am creating “Small Multiples”—multiple charts in one figure, where each chart needs its own background shade.

Method 3: Use Hex Codes for Professional Branding

While Python supports named colors like ‘red’ or ‘blue’, I almost always use Hex codes.

Most US marketing teams provide a specific color palette. Hex codes allow you to match those colors exactly in your Matplotlib pie charts.

Python Code Example: US Smartphone OS Preferences

import matplotlib.pyplot as plt

# Smartphone OS Market Share in the US
os_labels = ['iOS', 'Android', 'Other']
share = [55, 44, 1]

# Specific brand-friendly hex colors
bg_color = '#2c3e50' # Dark Midnight Blue
text_color = 'white'

fig, ax = plt.subplots(figsize=(8, 8), facecolor=bg_color)

# Creating the pie chart
wedges, texts, autotexts = ax.pie(share, labels=os_labels, 
                                  autopct='%1.1f%%', 
                                  colors=['#007aff', '#3ddc84', '#999999'])

# Changing label colors to be visible on the dark background
for text in texts:
    text.set_color(text_color)
for autotext in autotexts:
    autotext.set_color(text_color)

plt.title('Smartphone OS Usage in the USA', color=text_color, fontsize=18)
plt.show()

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

Pie Chart Background Color in Matplotlib

Whenever I use a dark background, I make sure to change the text color to white. This ensures the labels remain readable for the audience.

Method 4: Handle Background Transparency (RGBA)

There are times when I need to save a pie chart as a PNG with a transparent background.

This is a common requirement when you want to overlay the Python-generated chart onto a complex website background or a textured PDF.

In Matplotlib, we can use RGBA values (Red, Green, Blue, Alpha). The ‘Alpha’ value controls the transparency.

Python Code Example: Save with Transparency

import matplotlib.pyplot as plt

# Data: US Household Spending Categories
categories = ['Housing', 'Transportation', 'Food', 'Healthcare', 'Others']
spending = [33, 16, 13, 8, 30]

fig, ax = plt.subplots(figsize=(8, 8))

# Setting alpha to 0 makes it fully transparent
fig.patch.set_alpha(0.0)
ax.patch.set_alpha(0.0)

ax.pie(spending, labels=categories, autopct='%1.1f%%', startangle=140)

# Important: You must set transparent=True when saving
plt.savefig('us_spending_chart.png', transparent=True)
plt.show()

I have used this trick countless times to create charts that float perfectly over a company’s branded background in a digital report.

Common Issues When Changing Backgrounds

One issue I frequently encounter is the “bounding box” of the title or legend overlapping with the background.

If you change the Figure background to black, but your legend has a white background, it will look disconnected.

I always recommend setting the frameon attribute of the legend to False or matching its facecolor to the Figure.

Summary of Python Matplotlib Background Methods

To make it easier for you to choose the right approach, I have compiled this table based on my experience.

MethodTarget AreaBest Use Case
plt.figure(facecolor='color')Entire CanvasChanging the outer border and overall background.
ax.set_facecolor('color')Plotting AreaHighlighting the area directly behind the pie chart.
fig.patch.set_alpha(0)TransparencyCreating charts for web overlays or complex layouts.
plt.style.use('dark_background')Global ThemeQuickly switching to a pre-defined dark Python theme.

In this article, I have shared several ways to change the background color of a Python Matplotlib pie chart. I have covered using the figure facecolor, the axes patch, and even handling transparency for professional exports.

I hope you found this tutorial helpful! If you have any questions or run into issues with your Python Matplotlib code, feel free to experiment with the different color parameters we discussed today.

You may also like to read the other Matplotlib tutorials:

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.