Open oldoc63 opened 1 year ago
We also want to be able to understand what each slice of the pie represents. To do this we can either:
One other useful labeling tool for pie charts is adding the percentage of the total that each slice occupies. Matplotlib can add this automatically with the keyword autopct
. We pass in string formatting instructions to format the labels how we want. Some common formats are:
'%0.2f' — 2 decimal places, like 4.08
'%0.2f%%' — 2 decimal places, but with a percent sign at the end, like 4.08%. You need two consecutive percent signs because the first one acts as an escape character, so that the second one gets displayed on the chart.
'%d%%' — rounded to the nearest int and with a percent sign at the end, like 4%.
If we want to display elements of a data set as proportions of a whole, we can use a pie chart. Pie charts are helpful for displaying data like:
In Matplotlib, you can make a pie chart with the command plt.pie, passing in the values you want to chart: