Skip to content
Advertisement

Matplotlib pie chart: Show both value and percentage

Here’s my current code

values = pd.Series([False, False, True, True])
v_counts = values.value_counts()
fig = plt.figure()
plt.pie(v_counts, labels=v_counts.index, autopct='%.4f', shadow=True);

Currently, it shows only the percentage (using autopct)

I’d like to present both the percentage and the actual value (I don’t mind about the position)

How to do it?

Thanks

Advertisement

Answer

Create your own formatting function. Note that you have to recalculate the actual value from the percentage in that function somehow

def my_fmt(x):
    print(x)
    return '{:.4f}%n({:.0f})'.format(x, total*x/100)


values = pd.Series([False, False, True, True, True, True])
v_counts = values.value_counts()
total = len(values)
fig = plt.figure()
plt.pie(v_counts, labels=v_counts.index, autopct=my_fmt, shadow=True);

enter image description here

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement