Chapter 11 · 9 levels
Charts with pandas and matplotlib
Line, bar, histogram, scatter and stacked charts drawn with pandas and matplotlib, starting from the question and checking the numbers behind every chart.
StartA table is something you read. A chart is something you see: a trend, a gap between two bars or a long tail jumps out in under a second. That’s why almost every analysis you hand over ends in a chart, and why this chapter is all about drawing the right one.
Every level starts from a question a colleague asks, because the question picks the chart. Change over time is a line. Comparing categories is a bar. How values spread out is a histogram. Whether two numbers move together is a scatter plot. What a total is made of is a stacked bar.
How charts work in this game. Your code runs real matplotlib in the browser. Import it with
import matplotlib.pyplot as plt (keep that line: it’s what tells the page to load the chart
library) and every chart you draw shows up under your result, as an image. The first chart on a
visit takes a few seconds while the library loads. The check looks at the data behind the chart,
which you store in result: the Series or DataFrame you plotted. A beautiful chart of the wrong
numbers is still wrong, and a correct table is the part a colleague can verify. Colors, titles and
labels are up to you, and the lessons show what makes a chart clear and honest.
The chapter ends with a boss level where you build a small marketing dashboard: four charts on one figure, ready to drop into a slide.
Levels
- Black Friday in one line
from Megan Collins, Head of Sales
matplotlib, line chart, fig ax, axhline, resample
- Which return reasons cost the most?
from Angela Rivera, Head of Customer Support
bar chart, barh, sort values, bar label, highlight
- The question picks the chart
from Priya Shah, Head of Data
chart choice, histogram, line chart, bar chart, scatter
- Are most orders small?
from Laura Bennett, Head of Finance
histogram, bins, distribution, mean vs median, np histogram
- Do pricier products sell fewer units?
from Thomas Whitaker, Head of Merchandising
scatter, polyfit, trend line, slope, alpha
- Did orders really grow seventeen times?
from Catherine Hale, Chief Executive Officer
honest charts, y axis zero, set ylim, chart checklist
- Is the app catching up with the website?
from Jordan Reyes, Head of Marketing
multi line, legend, wide table, pivot table, pandas plot
- How the channel mix shifted
from Robert Kim, Chief Operating Officer
stacked bar, grouped bar, shares, crosstab, composition
- Boss: the marketing dashboard
from Jordan Reyes, Head of Marketing
subplots, dashboard, line chart, bar chart, cost per conversion