Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use matplotlib autopct?

I'd like to create a matplotlib pie chart which has the value of each wedge written on top of the wedge.

The documentation suggests I should use autopct to do this.

autopct: [ None | format string | format function ] If not None, is a string or function used to label the wedges with their numeric value. The label will be placed inside the wedge. If it is a format string, the label will be fmt%pct. If it is a function, it will be called.

Unfortunately, I'm unsure what this format string or format function is supposed to be.

Using this basic example below, how can I display each numerical value on top of its wedge?

plt.figure() values = [3, 12, 5, 8]  labels = ['a', 'b', 'c', 'd']  plt.pie(values, labels=labels) #autopct?? plt.show() 
like image 283
Kim Avatar asked May 29 '11 20:05

Kim


2 Answers

You can do:

plt.pie(values, labels=labels, autopct=lambda p : '{:.2f}%  ({:,.0f})'.format(p,p * sum(values)/100)) 
like image 40
caydin Avatar answered Sep 30 '22 14:09

caydin


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 '%.2f'%pct.

import matplotlib.pyplot as plt plt.figure() values = [3, 12, 5, 8]  labels = ['a', 'b', 'c', 'd']  plt.pie(values, labels=labels, autopct='%.2f') plt.show() 

yields Simple pie chart with percentages

You can do fancier things by supplying a callable to autopct. To display both the percent value and the original value, you could do this:

import matplotlib.pyplot as plt  # make the pie circular by setting the aspect ratio to 1 plt.figure(figsize=plt.figaspect(1)) values = [3, 12, 5, 8]  labels = ['a', 'b', 'c', 'd']   def make_autopct(values):     def my_autopct(pct):         total = sum(values)         val = int(round(pct*total/100.0))         return '{p:.2f}%  ({v:d})'.format(p=pct,v=val)     return my_autopct  plt.pie(values, labels=labels, autopct=make_autopct(values)) plt.show() 

Pie chart with both percentages and absolute numbers.

Again, for each pie wedge, matplotlib supplies the percent value pct as the argument, though this time it is sent as the argument to the function my_autopct. The wedge label is set to my_autopct(pct).

like image 169
unutbu Avatar answered Sep 30 '22 13:09

unutbu