Python - How to change the color of auto-update text to white in a pie chart?

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

I have a chart that displays the way I want it, except that the text will stand out better within the plot if it is white rather than black.

+5
source share
2 answers

From pyplot.pie documentation :

Return value:

If autopct is not None, return the tuple (patches, texts, autotests), where corrections and texts are indicated above, and autotexts is the list of Text instances for numerical 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 gives (with an example of Hogs and Dogs ): enter image description here

+10
source

You can do this on a single line using the 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 interesting example can be found here .

0
source

Source: https://habr.com/ru/post/1210891/


All Articles