liveBook · Manning

Pie chart

As explained in Pandas Workout: 200 exercises to make you a stronger data analyst

A pie chart displays parts of a whole as sectors whose angular sizes represent category proportions.

A pie chart treats a total like a whole pie: each category becomes a sector, and its angular size shows that category’s share of the total. Use it when comparing parts of one meaningful whole, rather than unrelated measurements. section 11.2

A practical pandas workflow is:

  • Filter the rows representing the population of interest. section 11.2
  • Select the category and numeric value columns. section 11.2
  • Set the category as the index and retrieve the numeric column as a Series. section 11.2
  • Call `.plot.pie()` and adjust the figure size when many sectors need room. section 11.2

Comparing Massachusetts city populations

After selecting Massachusetts rows, keep `city` and `population`, make `city` the index, and plot the resulting population Series: `df.loc[df['state'] == 'Massachusetts', ['city', 'population']].set_index('city')['population'].plot.pie(figsize=(10, 10))`. The chart shows Boston dominating the urban population represented in the dataset, followed by Worcester and Springfield. section 11.2 section 11.1

Institution types offering computer science

Group undergraduate computer-science institutions by `CONTROL`, count them, and plot the counts: `fields_of_study_df.loc[(fields_of_study_df['CIPDESC'].str.contains('Computer Science')) & (fields_of_study_df['CREDDESC'] == 'Bachelors Degree'), ['CONTROL', 'INSTNM']].groupby('CONTROL').count()['INSTNM'].plot.pie()`. This turns a summary table into a direct comparison of institutional shares. section 13.3

Interpret sectors as proportions of the supplied total, not as the entire real-world population unless the data represents that whole. Extend the exercise by changing the filter, grouping categories differently, or comparing the result with a bar chart. section 11.1 section 13.3

How Pandas Workout: 200 exercises to make you a stronger data analyst covers it

How deep it goes
Explains proportions and demonstrates filtering, indexing, grouping, counting, and plotting, but does not develop chart-design theory.
Examples
Uses Massachusetts city populations and institutional control categories for computer-science programs.
What it assumes
Assumes familiarity with pandas data frames, column selection, filtering, indexing, and groupby aggregation.
What you can do after
Apply it via pandas to convert filtered or grouped Series into proportional sector charts.
How it connects
Connects pie charts with pandas Series, filtering, groupby, proportions, bar charts, and scatter plots.

Also explained in

Compare how 3 books explain pie chart →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pandas Workout: 200 exercises to make you a stronger data analyst. The section links point to the parts of the book it draws on.