Visualization Supporting Materials#
1import pandas as pd
2import matplotlib.pyplot as plt
3import seaborn as sns
Anscombe#
In 1973, statistician Francis J. Anscombe published a brief but highly influential paper titled “Graphs in Statistical Analysis” to challenge the prevailing notion that numerical summary statistics alone could fully describe a dataset. To make his point, Anscombe constructed a set of four small datasets, now known as Anscombe’s Quartet, that share virtually identical summary statistics (means, variances, correlation coefficients, and even the same linear regression line) yet reveal distinctly different structures when plotted graphically. These plots demonstrate that graphs are not mere decoration; they are essential tools for understanding the true nature of your data.
Anscombe, F. J. “Graphs in Statistical Analysis.” The American Statistician 27, no. 1 (1973): 17–21. https://doi.org/10.2307/2682899.
1x = pd.Series([10.0, 8.0, 13.0, 9.0, 11.0, 14.0, 6.0, 4.0, 12.0, 7.0, 5.0], name="x1-3")
2x4 = pd.Series([ 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 19.0,8.0,8.0,8.0], name="x4")
3
4y1 = pd.Series([ 8.04, 6.95, 7.58, 8.81, 8.33, 9.96, 7.24, 4.26, 10.84, 4.82, 5.68], name="y1")
5y2 = pd.Series([9.14, 8.14, 8.74, 8.77, 9.26, 8.10, 6.13, 3.10, 9.13, 7.26,4.74], name="y2")
6y3 = pd.Series([7.46, 6.77, 12.74, 7.11, 7.81, 8.84, 6.08, 5.39, 8.15, 6.42, 5.73], name="y3")
7y4 = pd.Series([6.58, 5.76, 7.71, 8.84, 8.47, 7.04, 5.25, 12.50, 5.56, 7.91, 6.89], name="y4")
8data = {
9 "x1-3": x,
10 "x4": x4,
11 "y1": y1,
12 "y2": y2,
13 "y3": y3,
14 "y4": y4
15}
1df = pd.DataFrame(data)
2df.index = list(range(1,12))
3df.describe()
| x1-3 | x4 | y1 | y2 | y3 | y4 | |
|---|---|---|---|---|---|---|
| count | 11.000000 | 11.000000 | 11.000000 | 11.000000 | 11.000000 | 11.000000 |
| mean | 9.000000 | 9.000000 | 7.500909 | 7.500909 | 7.500000 | 7.500909 |
| std | 3.316625 | 3.316625 | 2.031568 | 2.031657 | 2.030424 | 2.030579 |
| min | 4.000000 | 8.000000 | 4.260000 | 3.100000 | 5.390000 | 5.250000 |
| 25% | 6.500000 | 8.000000 | 6.315000 | 6.695000 | 6.250000 | 6.170000 |
| 50% | 9.000000 | 8.000000 | 7.580000 | 8.140000 | 7.110000 | 7.040000 |
| 75% | 11.500000 | 8.000000 | 8.570000 | 8.950000 | 7.980000 | 8.190000 |
| max | 14.000000 | 19.000000 | 10.840000 | 9.260000 | 12.740000 | 12.500000 |
1import numpy as np
2import matplotlib.pyplot as plt
3
4fig = plt.figure(figsize=(8, 8), layout="constrained")
5
6# Common x-range for regression line
7x_line = np.linspace(0, 20, 100)
8
9# Plot 1
10ax1 = fig.add_subplot(2, 2, 1)
11ax1.scatter(df["x1-3"], y1)
12m, b = np.polyfit(df["x1-3"], y1, 1)
13ax1.plot(x_line, m * x_line + b)
14ax1.set_xlim(0, 20); ax1.set_ylim(0, 20)
15
16# Plot 2
17ax2 = fig.add_subplot(2, 2, 2)
18ax2.scatter(df["x1-3"], y2)
19m, b = np.polyfit(df["x1-3"], y2, 1)
20ax2.plot(x_line, m * x_line + b)
21ax2.set_xlim(0, 20); ax2.set_ylim(0, 20)
22
23# Plot 3
24ax3 = fig.add_subplot(2, 2, 3)
25ax3.scatter(df["x1-3"], y3)
26m, b = np.polyfit(df["x1-3"], y3, 1)
27ax3.plot(x_line, m * x_line + b)
28ax3.set_xlim(0, 20); ax3.set_ylim(0, 20)
29
30# Plot 4
31ax4 = fig.add_subplot(2, 2, 4)
32ax4.scatter(df["x4"], y4)
33m, b = np.polyfit(df["x4"], y4, 1)
34ax4.plot(x_line, m * x_line + b)
35ax4.set_xlim(0, 20); ax4.set_ylim(0, 20)
36
37plt.show()