liveBook · Manning

Grouped bar chart

As explained in Statistics Every Programmer Needs

A grouped bar chart is a bar chart that places multiple bars together for each category to compare subcategories.

A grouped bar chart places multiple bars beside one another for each category, making subcategory counts directly comparable. It supports exploratory analysis of relationships such as quarter and conversion outcome. section 6.2.3

Build one from grouped categorical counts:

  • Group observations by a main category and outcome, then reshape with unstack(fill_value=0). section 6.2.3
  • Plot the reshaped data with plot(kind='bar'), annotate bar heights, label axes, and name the outcome legend. section 6.2.3
  • Interpret differences as associations rather than causal effects; play type may reflect yards required for conversion. section 6.2.3
>>> import matplotlib.pyplot as plt     #1
>>> grouped_data = (nfl_pbp.groupby(['QUARTER', 'CONVERT']) \
>>>         .size().unstack(fill_value = 0)) #2
>>> grouped_data.plot(kind = 'bar')    #3






>>> for p in ax.patches:
>>>     ax.annotate(str(p.get_height()), 
>>>                 (p.get_x() + p.get_width() / 2., p.get_height()), 
>>>                 ha = 'center', va = 'bottom', 
>>>                 fontweight = 'bold', 
>>>                 color = 'black')     #4
>>> plt.title('Observation Counts by Quarter')     #5
>>> plt.xlabel('Quarter')     #6
>>> plt.ylabel('Count')    #7
>>> plt.xticks(rotation = 0)     #8
>>> plt.legend(title = 'CONVERT', 
>>>            labels = ['No', 'Yes'])    #9
>>> plt.show()    #10

Annotated Python listing showing grouped counts, bar plotting, labels, and value annotations. section 6.2.3

How Statistics Every Programmer Needs covers it

How deep it goes
Explains construction, annotation, labeling, and interpretation, but does not develop statistical testing or chart-design theory.
Examples
Compares fourth-down conversion counts by quarter, venue, and play type using annotated charts.
What it assumes
Assumes tabular observations and categorical variables, while demonstrating grouping, reshaping, plotting, and annotation from the workflow.
What you can do after
Build it from scratch with pandas grouping and Matplotlib bar plotting, then interpret subgroup count differences cautiously.
How it connects
Connects categorical grouping with pandas, Matplotlib, exploratory data analysis, and decision trees.

Also explained in

Compare how 2 books explain grouped bar chart →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Every Programmer Needs. The section links point to the parts of the book it draws on.