Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib percent label position in pie chart

Is there a way to change the default position of the percent label in a matplot lib pie chart?

Here is an example pie chart:

My pie chart

Which I have created using:

plt.pie(sizes, labels=labels, colors=colors, explode=explode, autopct='%1.0f%%') 

Now I don't like how some percent labels are intruding on other sections teritory (actually the only perpitrator in this example is the 9m section). Ideally I would like such labels to be outside the pie chart with an arrow of some sort pointing to the section, or alternativly just outside the section.

like image 951
Daniel Avatar asked Feb 05 '14 09:02

Daniel


People also ask

How do you plot percentages on a pie chart in Python?

To add percentages to each of the constitutents of the pie chart, we add in the line, autopct='%1.1f%%', to the plt. pie() function. This formats the percentage to the tenth place.

How do you avoid overlapping of labels and Autopct in a Matplotlib pie chart?

Use legend() method to avoid overlapping of labels and autopct. To display the figure, use show() method.

What is Autopct Python?

autopct enables you to display the percent value using Python string formatting. For example, if autopct='%. 2f' , then for each pie wedge, the format string is '%. 2f' and the numerical percent value for that wedge is pct , so the wedge label is set to the string '%.


1 Answers

You can control the distance of the percents and labels from the center of the pie using pctdistance= and labeldistance=, try this on your code:

plt.pie(sizes, labels=labels, autopct='%1.0f%%', pctdistance=1.1, labeldistance=1.2) 

You can also set a radius of the pie using radius= (by default is 1)

like image 58
Alvaro Fuentes Avatar answered Sep 17 '22 02:09

Alvaro Fuentes