Unit 10.02: Faceting instead of overplotting
Six series on one axis is a tangle. Six panels is a comparison.
Faceting, with a shared axis
Six groups drawn as six small panels sharing a y-axis.
The code builds the grid and confirms the axes are shared.
import matplotlib
matplotlib.use("Agg")
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
rows = []
for region in list("ABCDEF"):
for month in range(1, 13):
rows.append({"region": region, "month": month,
"revenue": 100 + month * (ord(region) - 64)})
df = pd.DataFrame(rows)
grid = sns.relplot(data=df, x="month", y="revenue", col="region",
col_wrap=3, kind="line", height=2)
print(f"regions : {df['region'].nunique()}")
print(f"panels drawn : {len(grid.axes.flat)}")
print(f"shared y-axis: {grid.axes.flat[0].get_ylim() == grid.axes.flat[1].get_ylim()}")
plt.close("all")
print("""
Six series on one axis is a tangle. Six small panels sharing a y-axis let the
reader compare shapes directly.
The shared axis is what makes the comparison valid -- panels with independent
scales look similar when they are not.
""")
The shared y-axis is what makes the comparison valid. Panels with independent scales look similar when they are not - each one fills its own space, so a small series and a large one appear identical.
That is the most common faceting error, and it is a default in several tools. Check it explicitly rather than assuming.
The mistake this prevents
The mistake is faceting by a variable with many levels. Twenty panels are as unreadable as twenty lines, just differently. Facet by something with a handful of levels, and use a different approach when there are more.
Takeaway
Facet instead of overplotting, and confirm the axes are shared. Independent scales make different-sized series look the same.
