Show value & % on pie chart


The following code shows a pie chart that currently displays percentages for the values in the values pd.Series:

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);

I would like to present both the percentage and the actual value in the pie chart.

To present both the percentage and the actual value in the pie chart, you can modify the autopct parameter to include both values. Here is the modified 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=lambda pct: f"{pct:.2f}%\n({int(pct/100*sum(v_counts))})",
        shadow=True, textprops=dict(color="w"));

This code uses a lambda function to format the autopct parameter. The pct parameter represents the percentage of the value, which is used to calculate the actual value by multiplying it with the sum of the v_counts values. The formatted string includes both the percentage and the actual value in parentheses. The textprops parameter is used to set the color of the text to white for better visibility.