How to change autopct text color to be white in a pie chart in Matplotlib?

To change the autopct text color to white in a pie chart in Matplotlib, you can modify the text properties of the percentage labels. This is useful when you have dark colored pie slices where white text provides better contrast and readability.

Steps to Change Autopct Text Color

  • Create the pie chart using plt.pie() with autopct parameter
  • Capture the returned autotext objects from the pie chart
  • Iterate through the autotext objects and set their color to white
  • Display the chart using show() method

Example

Here's how to create a pie chart with white percentage text −

import matplotlib.pyplot as plt

# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Data for pie chart
hours = [8, 1, 11, 4]
activities = ['sleeping', 'exercise', 'studying', 'working']
colors = ["grey", "green", "orange", "blue"]

# Create pie chart and capture autotext objects
_, _, autotexts = plt.pie(hours, labels=activities, colors=colors, autopct="%.2f%%")

# Change autopct text color to white
for autotext in autotexts:
    autotext.set_color('white')

plt.title("Daily Activities Distribution")
plt.show()

Understanding the Code

The plt.pie() function returns three objects: wedges, texts (labels), and autotexts (percentage labels). By unpacking with _, _, autotexts, we only capture the autotext objects that contain the percentage values.

Each autotext object has a set_color() method that allows you to change the text color. You can use any valid Matplotlib color specification like 'white', '#FFFFFF', or (1, 1, 1).

Additional Customization Options

You can further customize the autopct text appearance −

import matplotlib.pyplot as plt

hours = [8, 1, 11, 4]
activities = ['sleeping', 'exercise', 'studying', 'working']
colors = ["grey", "green", "orange", "blue"]

_, _, autotexts = plt.pie(hours, labels=activities, colors=colors, autopct="%.1f%%")

# Customize autopct text
for autotext in autotexts:
    autotext.set_color('white')
    autotext.set_fontsize(12)
    autotext.set_weight('bold')

plt.title("Daily Activities with Custom Text Style")
plt.show()

Conclusion

Changing autopct text color to white improves readability on dark pie slices. Use the autotext objects returned by plt.pie() and apply set_color('white') to achieve this customization.

Updated on: 2026-03-25T23:46:12+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements