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


To change the autopct text color to be white in a pie chart in Matplotlib, we can take the following steps −

  • Set the figure size and adjust the padding between and around the subplots.
  • Make a list of hours, activities, and colors to plot pie chart.
  • Make a list of '.Text' instances for the numeric labels, while making the pie chart.
  • Iterate autotexts and set the color of autotext as white.
  • To display the figure, use show() method.

Example

import matplotlib.pyplot as plt

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

plt.figure()

hours = [8, 1, 11, 4]
activities = ['sleeping', 'exercise', 'studying', 'working']
colors = ["grey", "green", "orange", "blue"]
_, _, autotexts = plt.pie(hours, labels=activities, colors=colors, autopct="%.2f")

for ins in autotexts:
    ins.set_color('white')

plt.show()

Output

Updated on: 08-Jul-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements