I would like to display actual value labels instead of %age values for a donut chart. The following code generates a pretty nice donut chart, but the displayed values are for %ages but not their actual values.
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
#create labels and values for pie/donut plot
labels = 'ms', 'ps'
sizes = [1851, 2230]
#create center white circle
centre_circle = plt.Circle((0, 0), 0.7, color='white')
#create pie chat
plt.pie(sizes, labels= labels, autopct='%1.1f%%',pctdistance = 1.25,startangle=90,
labeldistance=.8, colors = ["tab:blue", "tab:orange"])
plt.axis('equal')
plt.gca().add_artist(centre_circle)
#Display
plt.show();
My output is shown below.
Can somebody guide me on how to show the actual values (1851, 2230) and not their percentage values on this plot? Alternatively, display both the %ages and their actual corresponding values (i.e., 1851, 45.4% and 2230, 54.6%)?
Summarizing my comments, try this:
import matplotlib.pyplot as plt
labels = 'ms', 'ps'
sizes = 1851, 2230
pcts = [f'{s} {l}\n({s*100/sum(sizes):.2f}%)' for s,l in zip(sizes, labels)]
width = 0.35
_, ax = plt.subplots()
ax.axis('equal')
pie, _ = ax.pie(
sizes,
startangle=90,
labels=pcts,
# labeldistance=.8,
# rotatelabels=True,
colors = ["tab:blue", "tab:orange"]
)
plt.setp(pie, width=width, edgecolor='white')
plt.show()
And here is the output of the code snippet above: