Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

pie(fbfrac,labels = fblabel,autopct='%1.1f%%',pctdistance=0.8,startangle=90,colors=fbcolor)

I have the chart displaying just as I want it, with the exception that the text will stand out better inside the plot if it is white instead of black.

like image 755
Jsuf Avatar asked Jan 12 '15 09:01

Jsuf


People also ask

How do you change the font color in a pie chart?

Right-click on one label to open the Annotation dialog. In this dialog, you can use the Font Color option to customize the color for current label. If the color has been set to Auto, it means the color of current label will follow the Font Color setting in the the Labels tab of Plot Details dialog.

How do I change the text color in a pie chart in MatPlotLib?

Firstly, import matplotlib. To add labels, create a list of labels. To change the colors of slices, use the colors argument and pass a list of colors to it. To plot a pie chart, use the pie() method. To change the properties of wedges, use the wedgeprops argument.


1 Answers

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.

like image 59
Shahnoza Avatar answered Oct 19 '22 10:10

Shahnoza