IFI 8410 — Session 9: Visualization and Descriptive Statistics
Describing data honestly
The problem
An average, a spread, a correlation. Exact, and fits in a sentence. Hides the shape: two very different columns can have the same mean.
A histogram, bars, a scatter plot, a line. Shows the shape. Cannot be quoted, and can mislead through the choice of an axis.
Three failures this session is organised around:
The tools
tables
mean, median, std, quantile, describe, corr, and per-group summaries.
arrays
The same reductions on arrays; np.quantile; np.geomspace for logarithmic bins.
drawing
Figures and axes; histograms, bars, scatter plots, lines; labels, scales, layout; saving to PNG, PDF, SVG.
seaborn and the .plot methods of pandas are conveniences built on matplotlib. What you learn about figures and axes applies to them too.
Concept 1
salaries.mean() # 47.8 nine employees, 38 to 60 thousand
salaries.median() # 47.0
# add the owner, at 400
salaries.mean() # 83.0 nine of ten now earn less than "the average"
salaries.median() # 48.5| Question | Use | Because |
|---|---|---|
| what does a typical one look like? | median | half are below, half above, whatever the extremes do |
| what is the total, spread evenly? | mean | mean × count is the total |
Incomes, prices, review counts and waiting times are skewed to the right. A large gap between mean and median is itself a finding.
Concept 2
waits.var() # variance
waits.std() # standard deviation| divides by | |
|---|---|
x.var() in pandas | count − 1 |
np.var(x) in NumPy | count |
np.var(x, ddof=1) | count − 1 |
q1 = values.quantile(0.25)
q3 = values.quantile(0.75)
iqr = q3 - q1
values.describe()| Centre | Spread | |
|---|---|---|
| built on sums | mean | variance, std |
| built on order | median | quartiles, IQR |
Same data, same function name, two answers, no warning. One extreme value moves everything built on sums and hardly touches what is built on order.
Concept 3
outside = (values < q1 - 1.5 * iqr) | (values > q3 + 1.5 * iqr)A column of True and False. Nothing is removed. A flagged value can be three different things:
| It is | Example | Do |
|---|---|---|
| a mistake | a salary typed with an extra zero | correct it or make it missing, and say so |
| a different kind of thing | the owner among the employees | decide whether it belongs to the question |
| the most important row | the customer who brings half the revenue | keep it and report it |
Concept 4
table["temperature"].corr(table["cups"]) # r, from -1 to 1Four small datasets with the same means, the same standard deviations, and the same correlation, 0.82.
a loose cloud, a curve, a line with one point off it, and a vertical stack with one point that creates the whole correlation.
Sums cannot tell shapes apart. Always draw the data.
Concept 4, continued

Same mean of x (9) and y (7.50), same variances, same r = 0.82, same line y = 3.00 + 0.500x.
Figure: Anscombe's quartet, Wikipedia (F. J. Anscombe, 1973).
Concept 5
fig, ax = plt.subplots(figsize=(7, 4)) # a Figure, with one Axes
ax.hist(waits, bins=20)
ax.axvline(waits.mean(), linestyle="--", label="mean")
ax.axvline(waits.median(), linestyle=":", label="median")
ax.set_xlabel("waiting time (minutes)")
ax.set_ylabel("customers")
ax.set_title("A typical wait is four minutes; a few wait three times as long")
ax.legend()
fig.savefig("waits.png", dpi=150, bbox_inches="tight")Concept 5, continued
def draw_waits(ax, waits):
ax.hist(waits, bins=20)
ax.set_xlabel("waiting time (minutes)")
return ax
fig, (left, right) = plt.subplots(1, 2, sharex=True, sharey=True)
draw_waits(left, january)
draw_waits(right, july)
fig.savefig("waits.png")
plt.close(fig)sharex and sharey give the panels the same range, which makes them comparable by eye.show().Concept 6
| Question | Kind | Chart | Method | Summary number |
|---|---|---|---|---|
| how are the values spread out? | distribution | histogram, box plot | ax.hist, ax.boxplot | median, quartiles |
| which group has more? | comparison | bars | ax.bar, ax.barh | one number per group |
| do two things go together? | relationship | scatter plot | ax.scatter | correlation |
| how did it change over time? | trend | line | ax.plot | change from start to end |
Most bad charts are a good answer to a question nobody asked.
Concept 7
A figure without axis labels and units is not yet a result.
np.geomspace, and a label that says so.Worked example — notebook 2
Complaints per company, 5,275 companies. The typical company received six complaints in seven years.
received half of all complaints.
The histogram shows nothing until the axis is logarithmic. Then the shape appears.
hold 96 % of the complaints. They are not errors. They are the subject.
Worked example — notebook 2
Equal-width bins on heavily skewed counts put nearly everything in the first bar.
"Credit reporting" was renamed partway through. Its complaints are split across two bars, and mortgages look like the top product.
The trend line collapses in the final month. The file ends on the tenth.
7,052 complaints were forwarded "one day early". Not 7,052 typing mistakes: one cause, such as two systems in different time zones. Decide, and write the count next to the result.
Also in the data: a spike that follows a data breach at a credit bureau, an outlier that is the most informative point on the chart; and a correlation near zero with a clear pattern all the same, since the companies that answer late are small ones.
To take away
Now: hands-on
Folder 09-Visualization-Descriptive-Statistics — work them in this order:
Concepts
Centre, spread, outliers, your first figure, and the coffee cart's summer in four charts. Four exercises.
Worked example
1.3 million records, seven steps, four traps. Run it on ARC.
Look things up
Statistics and plotting calls to keep open while you work.
As you work, ask of every figure: would a reader get the right numbers from it?
Homework: Homework 8: Same Numbers, Different Pictures

