Unit 10.00: What Seaborn does that Matplotlib does not
Seaborn's value is the number of decisions it makes for you, which is also its cost.
One call, four decisions
A grouped line chart built with a single Seaborn call.
The code reports what it produced without being asked.
import matplotlib
matplotlib.use("Agg")
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.DataFrame({"region": ["N", "N", "S", "S"] * 10,
"month": list(range(1, 21)) * 2,
"revenue": list(range(100, 120)) + list(range(90, 110))})
ax = sns.lineplot(data=df, x="month", y="revenue", hue="region")
print("one call produced:")
print(f" lines : {len([l for l in ax.lines if l.get_label()[0] != '_'])}")
print(f" legend : {ax.get_legend() is not None}")
print(f" axis labels : {ax.get_xlabel()!r}, {ax.get_ylabel()!r}")
plt.close("all")
print("""
Seaborn read the column names for the labels, split by `region` on its own,
and built the legend. In Matplotlib each of those is a separate call.
What you give up is control: those defaults are opinions, and the next unit
is about the ones you should not accept silently.
""")
It read the column names for the axis labels, split the data by the grouping column, chose colours, and built the legend. In Matplotlib each of those is a separate call.
The trade is control. Those defaults are opinions - some of them statistical, which is the subject of the fourth unit in this module - and accepting them silently means shipping opinions you did not examine.
The mistake this prevents
The mistake is choosing between the libraries. Seaborn returns Matplotlib objects, so the useful pattern is Seaborn for the statistical work and Matplotlib for the final control - which the last unit of this module demonstrates.
Takeaway
Seaborn makes several decisions per call, including statistical ones. Use it for the exploration and take control back for the deliverable.
