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

You can do it in one line using textprops argument of pyplot.pie. It's simple:

plt.pie(data, autopct='%1.1f%%', textprops={'color':"w"})

In your case:

pie(fbfrac, labels=fblabel, autopct='%1.1f%%', pctdistance=0.8, startangle=90, colors=fbcolor, textprops={'color':"w"})

An enlightening example can be found here.


Pie object returns patches, texts, autotexts. You can loop through the texts and autotext and set_color.

import matplotlib.pyplot as plt

fblabels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
fbfrac = [15, 30, 45, 10]
fbcolor = ["blue", "green", "red", "orange"]


fig, ax = plt.subplots()
patches, texts, autotexts  = ax.pie(fbfrac, labels = fblabels, autopct='%1.1f%%',pctdistance=0.8,startangle=90,colors=fbcolor)
[text.set_color('red') for text in texts]
texts[0].set_color('blue')
[autotext.set_color('white') for autotext in autotexts]

plt.show()

Output

Moreover you can change the color for individual label, accessing the list item, e.g: texts[0].set_color('blue')

You can get more inspiration here.


From pyplot.pie documentation:

Return value:

If autopct is not None, return the tuple (patches, texts, autotexts), where patches and texts are as above, and autotexts is a list of Text instances for the numeric labels.

You need to change the color of autotexts; this is done simply by set_color():

_, _, autotexts = pie(fbfrac,labels = fblabel,autopct='%1.1f%%',pctdistance=0.8,startangle=90,colors=fbcolor)
for autotext in autotexts:
    autotext.set_color('white')

This yields (with Hogs and Dogs example): enter image description here