oldoc63 / learningDS

Learning DS with Codecademy and Books
0 stars 0 forks source link

Pie Chart #488

Open oldoc63 opened 1 year ago

oldoc63 commented 1 year ago

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:

oldoc63 commented 1 year ago
  1. MatplotSip keeps track of how many people pay by credit card, cash, Apple pay, or other methods. This is given to you in the payment_methods_names and payment_methods_freqs lists. Display the payment_method_freqs list as a pie chart.
oldoc63 commented 1 year ago

Pie Chart Labeling

We also want to be able to understand what each slice of the pie represents. To do this we can either:

  1. Use a legend to label each color
  2. Put labels on the chart itself
oldoc63 commented 1 year ago

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%.
oldoc63 commented 1 year ago
  1. Add a legend to the chart you made in the previous exercise by passing in a list of labels to plt.legend. For the labels, use the list payment_methods_names.
oldoc63 commented 1 year ago
  1. Add a percentage to each slice using Matplotlib's autopct parameter. Go to one decimal point of precision.