Search code examples
python-3.xseriespie-chart

How to print percentages outside the pie with python


I would like to have percentage values outside the pie. Maybe you can help

Here is my code :

import matplotlib.pyplot as plt
import pandas as pd

dict={'a':45, 'b': 123, 'c':2, 'd':1755, 'e':13}
ser = pd.Series(dict)

print(ser)

ser.plot(kind='pie', shadow=True, autopct='%1.2f%%')
plt.show()

As you can see in my case percentage values are not visible


Solution

  • According to the docs pctdistance controls the "ratio between the center of each pie slice and the start of the text generated by autopct", and it has a default value of 0.6, which causes the percentage values to be inside the pie.

    Try a pctdistance value > 1:

    >>> ser.plot(kind='pie', shadow=True, autopct='%1.2f%%', pctdistance=1.15)
    

    (The above results in an "ugly" plot in which the percentages and the labels overlap. You can fix that by "moving" the labels using labeldistance.)